【发布时间】:2013-10-31 01:49:43
【问题描述】:
我有一个类似这样的控制器规格
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/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
【问题讨论】:
-
在我看来一切都是合法的。您是否正确准备了测试数据库,即。
rake db:test:prepare? -
是的。之前做过
RAILS_ENV=test rake db:wipe db:migrate db:seed。 (db:wipe 是一个自定义任务,只删除所有表) -
你能尝试实际运行
rake db:test:prepare吗? -
@sevenseacat 完成。没有变化。
标签: ruby-on-rails ruby factory-bot rspec2