Rspec, Rails - unable to account for controller variable in testing -
i new testing. don't seem able account variable in controller in rspec tests. how can account needed variable in tests , make these tests pass?
current test:
describe "post create action" let(:trip) { create(:trip)} let(:trip_date) { create(:trip_date) } let(:buyer) { create(:buyer) } let(:company) { create(:company) } let(:order_item) { attributes_for(:order_item, trip_date_id: trip_date.id, buyer_id: buyer.id, company_id: company.id) } let(:bad_order_item) { attributes_for(:bad_order_item, trip_date_id: trip_date.id, buyer_id: buyer.id, company_id: company.id) } context "given valid order item attributes" "creates new order item" expect{ post :create, order_item: order_item, trip_id: trip.id }.to change(orderitem, :count).by(1) end end end
this error message:
orderitemscontroller post create valid attributes creates new order item failure/error: expect{ post :create, order_item: factorygirl.attributes_for(:order_item) }.to change(orderitem,:count).by(1) nomethoderror: undefined method `company_id' nil:nilclass # ./app/controllers/order_items_controller.rb:29:in `create' # ./spec/controllers/order_items_controller_spec.rb:47:in `block (4 levels) in <top (required)>'
error references line 29 of order_items_controller.rb:
line 25: def create line 26: @trip = trip.friendly.find_by_id(params[:trip_id) line 27: @order_item = orderitem.new(order_item_params) line 28: @order_item.buyer_id = current_user.id line 29: @order_item.company_id = @trip.company_id line 30: @order_item.first_person_cost = @trip.first_person_cost line 31: @order_item.second_person_cost = @trip.second_person_cost line 32: if @order_item.save line 33: redirect_to cart_path(current_user), notice: 'new order item created.' line 34: else line 35: render 'new', notice: 'unable create new order item.' line 36: end line 37: end
the callback affecting controller action confirms user signed in before action begins. if not, redirected sign in page.
i have tried: let(:trip) { create(:trip)} let(:trip_date) { create(:trip_date) } let(:buyer) { create(:buyer) } let(:company) { create(:company) } let(:order_item) { attributes_for(:order_item, trip_date_id: trip_date.id, buyer_id: buyer.id, company_id: company.id) } let(:bad_order_item) { attributes_for(:bad_order_item, trip_date_id: trip_date.id, buyer_id: buyer.id, company_id: company.id) }
describe "post create" let(:trip) {create(:trip) } context "with valid attributes" "creates new order item" trip.stub_chain(:friendly, :find_by_id).and_return(trip) expect{ post :create, order_item: order_item }.to change(orderitem,:count).by(1) end end end end
resulting in error of:
1) orderitemscontroller post create action given valid order item attributes creates new order item failure/error: let(:trip) { create(:trip)} double received unexpected message :where (no args) # ./spec/controllers/order_items_controller_spec.rb:34:in `block (3 levels) in <top (required)>'
additionally, documentation on specific rpsec code can hard find. i'd appreciate being pointed in right direction toward docs rspec's 'post' method.
thanks in advance.
you can't assign variables in spec , them in controller!
there several ways how test it
1 controller finds record in db - create trip.friendly.find_by_id(params[:trip_id)
return it
describe "post create" let(:trip) { factorygirl.create(:trip) } context "with valid attributes" "creates new order item" order_item = factorygirl.attributes_for(:order_item) expect{ post :create, order_item: order_item, trip_id: trip.id }.to change(orderitem,:count).by(1) end end end
2 use stub
(or stub_chain
)
describe "post create" let(:trip) { factorygirl.create(:trip) } context "with valid attributes" "creates new order item" trip.stub_chain(:friendly, :find_by_id).and_return(trip) order_item = factorygirl.attributes_for(:order_item) expect{ post :create, order_item: order_item }.to change(orderitem,:count).by(1) end end end
ps me first variant better, because controller accept params[:trip_id]
better pass it.
Comments
Post a Comment