Ruby on rails 未定义nil的工厂女孩方法:NilClass
我有一个类似这样的控制器规范Ruby on rails 未定义nil的工厂女孩方法:NilClass,ruby-on-rails,ruby,factory-bot,rspec2,Ruby On Rails,Ruby,Factory Bot,Rspec2,我有一个类似这样的控制器规范 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
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
我的CreditCardAccount是空的ActiveRecord::基本模型
=> 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/ruby-2.0.0-p247@complex_finance/gems/rails-4.0.0
/home/brundage/.rvm/gems/ruby-2.0.0-p247@complex_finance/gems/rspec-rails-2.14.0
/home/brundage/.rvm/gems/ruby-2.0.0-p247@complex_finance/gems/factory_girl-4.2.0
这应该是有效的。测试数据库不正确的所有要点
RAILS\u ENV=test rake db:drop db:create
将删除并重新创建测试数据库。然后尝试使用rake命令运行rspec,以便迁移数据库:rake rspec
我遇到了相同的问题,但我认为问题的原因不同。然而,我的解决方案可能有用:我使用了制造gem()而不是FG
我遇到这个问题的原因是因为我试图让FG创建/构建一个不是ActiveRecord的对象,它只是一个ActiveModel,并且必须用参数初始化它
我在制造商文档中没有看到一个完全符合我需要的示例,但我用以下语法得到了它:
制造商(:我的班级)做什么
论我的初衷
带(“公司名称”、“假第二个参数”)的初始值
结束
结束我的问题是,在模型中,我创建了一个名为
:send的私有方法(忘记了它已经在Ruby中使用) 谢谢,但不是这样。@Deanbrindage抱歉。将让答案有可能帮助某人,谷歌在这里。您能在测试数据库中看到此列吗?你的db/schema.rb
正确吗?请看我对你答案的答复。我觉得一切都是合法的。您是否正确准备了测试数据库,即rake db:test:prepare
?是的。之前做过一次rake-db:wipe-db:migrate-db:seed测试。(db:wipe是一个只删除所有表的自定义任务)您是否可以尝试实际运行rake db:test:prepare
?@sevensacat Done。没有变化。
0 HAL:0 work/complex_finance % bundle show rails rspec-rails factory_girl
/home/brundage/.rvm/gems/ruby-2.0.0-p247@complex_finance/gems/rails-4.0.0
/home/brundage/.rvm/gems/ruby-2.0.0-p247@complex_finance/gems/rspec-rails-2.14.0
/home/brundage/.rvm/gems/ruby-2.0.0-p247@complex_finance/gems/factory_girl-4.2.0