[英]RSpec Couldn't find with 'id'=
I'm writing an RSpec controller test and I've run into the following problem. 我正在编写RSpec控制器测试,但遇到了以下问题。
The relationship is such that Invoices belong to a Purchase, and a Purchase has many invoices. 这种关系使得发票属于采购,并且采购具有许多发票。
My controller has: 我的控制器有:
class InvoicesController < ApplicationController
def index
@invoices = Invoice.all
end
def new
@purchase = Purchase.find(params[:purchase])
@invoice = Invoice.new(:purchase_id => params[:purchase])
end
My factory has: 我的工厂有:
FactoryGirl.define do
factory :invoice do |f|
sequence(:id) { |number| number }
f.purchase_id {rand(1..30)}
f.number { FFaker::String.from_regexp(/\A[A-Za-z0-9]+\z/) }
f.terms { FFaker::String.from_regexp(/\A[A-Za-z0-9\s]+\z/) }
f.currency { FFaker::String.from_regexp(/\A[A-Z]+\z/) }
f.total_due {'25000.00'}
f.current_balance {'12500.00'}
f.due_date { FFaker::Time.date }
f.notes { FFaker::HipsterIpsum.paragraph }
f.status {[:open, :paid, :canceled].sample}
purchase
end
factory :invalid_invoice, parent: :invoice do |f|
f.status nil
end
end
My controller spec (just the problematic part) has: 我的控制器规格(仅是有问题的部分)具有:
describe "GET new" do
it "assigns a new invoice to @invoice" do
invoice = FactoryGirl.create(:invoice)
get :new
expect(assigns(:invoice)).to_not eq(invoice)
end
it "renders the :new template" do
get :new
expect(response).to render_template :new
end
end
In my routes I have: 在我的路线中,我有:
purchases GET /purchases(.:format) purchases#index
POST /purchases(.:format) purchases#create
new_purchase GET /purchases/new(.:format) purchases#new
edit_purchase GET /purchases/:id/edit(.:format) purchases#edit
purchase GET /purchases/:id(.:format) purchases#show
PATCH /purchases/:id(.:format) purchases#update
PUT /purchases/:id(.:format) purchases#update
DELETE /purchases/:id(.:format) purchases#destroy
invoices GET /invoices(.:format) invoices#index
POST /invoices(.:format) invoices#create
new_invoice GET /invoices/new(.:format) invoices#new
edit_invoice GET /invoices/:id/edit(.:format) invoices#edit
invoice GET /invoices/:id(.:format) invoices#show
PATCH /invoices/:id(.:format) invoices#update
PUT /invoices/:id(.:format) invoices#update
DELETE /invoices/:id(.:format) invoices#destroy
When I run the test I get this: 当我运行测试时,我得到以下信息:
1) InvoicesController GET new assigns a new invoice to @invoice
Failure/Error: get :new
ActiveRecord::RecordNotFound:
Couldn't find Purchase with 'id'=
# ./app/controllers/invoices_controller.rb:7:in `new'
# ./spec/controllers/invoices_controller_spec.rb:38:in `block (3 levels) in <top (required)>'
2) InvoicesController GET new renders the :new template
Failure/Error: get :new
ActiveRecord::RecordNotFound:
Couldn't find Purchase with 'id'=
# ./app/controllers/invoices_controller.rb:7:in `new'
# ./spec/controllers/invoices_controller_spec.rb:43:in `block (3 levels) in <top (required)>'
Here is a snippet from test.log 这是来自test.log的片段
[1m[36m (0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m [1m[35m (0.1ms)[0m SAVEPOINT active_record_1 [1m[36mSQL (0.3ms)[0m [1mINSERT INTO "purchases" ("id", "vendor_id", "order_number", "status", "notes", "tradegecko_url", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"[0m [["id", 4], ["vendor_id", 4], ["order_number", "zzz"], ["status", "canceled"], ["notes", "Jean shorts cliche Williamsburg raw denim put a bird on it messenger bag. Shoreditch keytar Brooklyn lomo brunch. Mcsweeney's Cosby Sweater +1 PBR Austin biodiesel freegan."], ["tradegecko_url", " http://gorczany.info "], ["created_at", "2016-07-19 14:51:00.616108"], ["updated_at", "2016-07-19 14:51:00.616108"]] [1m[35m (0.1ms)[0m RELEASE SAVEPOINT active_record_1 [1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m [1m[35mSQL (0.3ms)[0m INSERT INTO "invoices" ("id", "purchase_id", "number", "terms", "currency", "total_due", "current_balance", "due_date", "notes", "status", "created_at", "upda [1m [36m(0.1ms)[0m [1m发布SAVEPOINT active_record_1 [0m [1m [35m(0.1ms)[0m SAVEPOINT active_record_1 [1m [36mSQL(0.3ms)[0m [1mINSERT INTO“ purchases”(“ id”,“ vendor_id”,“ order_number”,“ status”,“ notes”,“ tradegecko_url”,“ created_at”,“ updated_at”)值($ 1,$ 2,$ 3,$ 4,$ 5,$ 6,$ 7,$ 8)返回“ id” [ 0m [[“ id”,4],[“ vendor_id”,4],[“ order_number”,“ zzz”],[“ status”,“ canceled”],[“ notes”,“牛仔短裤cliche Williamsburg raw denim穿上它斜挎包一只鸟。肖尔迪奇keytar布鲁克林LOMO早午餐。斯威尼的考斯比毛衣+1 PBR奥斯汀生物柴油freegan。 “],[” tradegecko_url “ ” http://gorczany.info “],[” created_at“,” 2016 -07-19 14:51:00.616108“],[” updated_at“,” 2016-07-19 14:51:00.616108“]] [1m [35m(0.1ms)[0m RELEASE SAVEPOINT active_record_1 [1m [36m(0.1 ms)[0m [1mSAVEPOINT active_record_1 [0m [1m [35mSQL(0.3ms)[0m插入“发票”(“ id”,“ purchase_id”,“ number”,“ terms”,“ currency”,“ total_due”,“ current_balance”,“ due_date”,“ notes”,“ status”,“ created_at”,“ upda” ted_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["id", 4], ["purchase_id", 4], ["number", "dd"], ["terms", "TT"], ["currency", "MM"], ["total_due", "25000.0"], ["current_balance", "12500.0"], ["due_date", "2015-11-13"], ["notes", "Scenester Carles cred quinoa fixie put a bird on it Four Loko next level. ted_at“)值($ 1,$ 2,$ 3,$ 4,$ 5,$ 6,$ 7,$ 8,$ 9,$ 10,$ 11,$ 12)返回” id“ [[” id“,4],[” purchase_id“,4], [“ number”,“ dd”],[“ terms”,“ TT”],[“ currency”,“ MM”],[“ total_due”,“ 25000.0”],[“ current_balance”,“ 12500.0”], [“ due_date”,“ 2015-11-13”],[“ notes”,“ Scenester Carles cred quinoa fixie在其下一个小鸟上放了四个Loko。 Biodiesel vice Wayfarers sustainable brunch butcher locavore. 生物柴油副食品Wayfarers可持续早午餐屠夫。 Keytar vice next level stumptown Rerry Richardson."], ["status", "canceled"], ["created_at", "2016-07-19 14:51:00.619066"], ["updated_at", "2016-07-19 14:51:00.619066"]] [1m[36m (0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m Processing by InvoicesController#new as HTML [1m[35mPurchase Load (0.3ms)[0m SELECT "purchases".* FROM "purchases" WHERE "purchases"."id" = $1 LIMIT 1 [["id", nil]] Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms) Keytar副级别下的树桩镇Rerry Richardson。“],[”状态“,”取消“],[” created_at“,” 2016-07-19 14:51:00.619066“],[” updated_at“,” 2016-07- 19 14:51:00.619066“]] [1m [36m(0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1 [0m InvoicesController#new作为HTML处理] [1m [35m购买负荷(0.3ms)[0m选择”购买“。*从“购买”位置“购买”。“ id” = $ 1 LIMIT 1 [[“ id”,nil]]在2ms内完成404找不到(ActiveRecord:0.3ms)
I think the problem is that the factory associations are created, but not saved. 我认为问题在于工厂关联已创建但未保存。 So when the purchase.id is called it returns nil. 因此,在调用purchase.id时,它返回nil。
you are trying to load a purchase in the new
action: 您正在尝试在new
操作中进行购买:
def new
@purchase = Purchase.find(params[:purchase])
@invoice = Invoice.new(:purchase_id => params[:purchase])
end
but in your test you are not passing a :purchase
parameter (should be :purchase_id i guess)! 但是在您的测试中,您没有传递:purchase
参数(我想应该是:purchase_id)!
The associations stated in the factories were not persisting, resulting in a nil value for the Purchase.id. 工厂中声明的关联没有持久存在,因此Purchase.id的值为零。
So after trying before callbacks, after callbacks, traits, I gave up on using FactoryGirl's auto-generated associations. 因此,在尝试回调之前,回调之后,特性之后,我放弃了使用FactoryGirl的自动生成的关联。
In the end this is what I had to do: 最后,这就是我要做的:
I removed all stated associations from the factories, then did it manually, the edited code in the spec: 我从工厂中删除了所有声明的关联,然后手动完成了规范中的已编辑代码:
describe "GET new" do
it "assigns a new invoice to @invoice" do
invoice = FactoryGirl.create(:invoice)
get :new, purchase: FactoryGirl.create(:purchase)
expect(assigns(:invoice)).to_not eq(invoice)
end
it "renders the :new template" do
get :new, purchase: FactoryGirl.create(:purchase)
expect(response).to render_template :new
end
end
I hope this helps anyone else who comes across this problem. 希望这对遇到此问题的其他人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.