我正在尝试测试一个“创建后”的帖子。控制器中的动作。在“创建”期间,在控制器中,我分配一个变量,其值在修改新记录之前保存该记录。该变量@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
其他ATTEMPTS
湾我也尝试过:
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
导致同样的错误。
℃。我也试过
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)>'
感谢任何帮助。已经过了几个星期了。感谢。
答案 0 :(得分:1)
我最后搜索了正确的术语,并找到了Rails文档,讨论如何为每个动作设置会话变量。我的解决方案看起来像这样:
expect{ post :create, {order_item: order_item}, {"current_trip_id" => trip.id} }.to change(OrderItem, :count).by(1)
答案 1 :(得分:0)
expect{ post :create, order_item: order_item, trip_id: trip.id }
尝试将其更改为:
expect{ post :create, { order_item: order_item, trip_id: trip.id } }
post
将params散列和会话散列作为参数;所有发布的参数都应该在一个哈希值中。