Reputation: 417
I'm writing an RSpec controller test and I've run into the following problem.
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
[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", "updated_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. Biodiesel vice Wayfarers sustainable brunch butcher locavore. 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)
I think the problem is that the factory associations are created, but not saved. So when the purchase.id is called it returns nil.
Upvotes: 0
Views: 2287
Reputation: 417
The associations stated in the factories were not persisting, resulting in a nil value for the Purchase.id.
So after trying before callbacks, after callbacks, traits, I gave up on using FactoryGirl's auto-generated associations.
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.
Upvotes: 0
Reputation: 18845
you are trying to load a purchase in the new
action:
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)!
Upvotes: 0