我有一個控制器的規格是這樣的爲無工廠女孩未定義的方法:NilClass
describe :bizzaro_controller do
let(:credit_card_account) { FactoryGirl.build :credit_card_account }
it "doesn't blow up with just the stub" do
CreditCardAccount.stub(:new).and_return(credit_card_account)
end
it "doesn't blow up" do
credit_card_account
CreditCardAccount.stub(:new).and_return(credit_card_account)
end
end
導致此:
bizzaro_controller
doesn't blow up with just the stub (FAILED - 1)
doesn't blow up
Failures:
1) bizzaro_controller doesn't blow up
Failure/Error: let(:credit_card_account) { FactoryGirl.build :credit_card_account }
NoMethodError:
undefined method `exp_month=' for nil:NilClass
# ./spec/controllers/user/bizzareo_controller_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/controllers/user/bizzareo_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.23631 seconds
2 examples, 1 failure
我的信用卡廠是這樣的:
FactoryGirl.define do
factory :credit_card_account do
exp_month 10
exp_year 2075
number '3'
end
end
我的CreditCardAccount是一個空的ActiveRecord :: Base模型
個=> CreditCardAccount(id: integer, exp_month: integer, exp_year: integer, number: string)
版本
0 HAL:0 work/complex_finance % bundle show rails rspec-rails factory_girl
/home/brundage/.rvm/gems/[email protected]_finance/gems/rails-4.0.0
/home/brundage/.rvm/gems/[email protected]_finance/gems/rspec-rails-2.14.0
/home/brundage/.rvm/gems/[email protected]_finance/gems/factory_girl-4.2.0
所有看起來都合法的我。你有沒有準備好你的測試數據庫,即。 'rake db:test:prepare'? – sevenseacat
是的。在執行'RAILS_ENV = test rake db:wipe db:migrate db:seed'之前。 (db:wipe是一個自定義任務,只是刪除所有的表) –
你可以嘗試實際運行'rake db:test:prepare'嗎? – sevenseacat