我正在尝试在控制器中测试“创建后”操作。在“创建”期间,在控制器中,我分配了一个变量,其值会在保存新记录之前修改该记录。该变量@trip 是从我似乎无法解释的会话值创建的。
所以我的问题是,鉴于我要测试的内容,当该变量由会话值设置时,说明控制器中的实例变量的最佳方式(或任何方式)是什么?
错误信息:
OrderItemsController POST create with valid attributes creates a 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' for 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)>'
控制器规格:
describe "POST create action" do
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" do
it "creates a new order item" do
expect{ post :create, { order_item: order_item, trip_id: trip.id } }.to change(OrderItem, :count).by(1)
end
end
end
错误引用了我的 order_items_controller.rb 的第 29 行,其中变量出现:
line 25: def create
line 26: @trip = Trip.find_by_id(session[:current_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 to create new order item.'
line 36: end
line 37: end
其他尝试
湾。我也试过:
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" do
let(:trip) {create(:trip) }
context "with valid attributes" do
it "creates a new order item" do
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
导致同样的错误。
C。我也试过
describe "POST create action" do
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" do
it "creates a new order item" do
expect{ post :create, order_item: order_item, trip_id: trip.id }.to change(OrderItem, :count).by(1)
end
end
end
导致同样的错误。
d。我也试过:
describe "POST create action" do
let(:trip_date) { create(:trip_date) }
let(:buyer) { create(:buyer) }
let(:company) { create(:company) }
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" do
it "creates a new order item" do
Trip.stub_chain(:friendly, :find_by_id).and_return(trip)
order_item = attributes_for(:order_item, trip_date_id: trip_date.id, buyer_id: buyer.id, company_id: company.id)
expect{ post :create, order_item: order_item }.to change(OrderItem, :count).by(1)
end
end
导致:
1) OrderItemsController POST create action given valid order item attributes creates a new order item
Failure/Error: Trip.stub_chain(:friendly, :find_by_id).and_return(trip)
NameError:
undefined local variable or method `trip' for #<RSpec::ExampleGroups::OrderItemsController_2::POSTCreateAction::GivenValidOrderItemAttributes:0x0000010154d5d8>
# ./spec/controllers/order_items_controller_spec.rb:41:in `block (4 levels) in <top (required)>'
任何帮助表示赞赏。已经有几个星期了。谢谢。