5

我有一个类似这样的控制器规格

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
4

3 回答 3

0

我有同样的问题,但我认为我的问题的原因是不同的。但是,我的解决方案可能有用:我使用了 Fabrication gem ( http://www.fabricationgem.org/ ) 而不是 FG。

我遇到这个问题的原因是因为我试图让 FG 创建/构建一个不是 ActiveRecord 的对象,它只是一个 ActiveModel,并且必须使用参数进行初始化。

我没有在 Fabricator 文档中看到完全符合我需要的示例,但我使用以下语法得到了它:

Fabricator(:my_class) do on_init do init_with("Company Name", "Fake second arg") end end

于 2014-04-21T05:55:11.570 回答
0

这应该有效。您的测试数据库不正确的所有点。

RAILS_ENV=test rake db:drop db:create将删除并重新创建您的测试数据库。然后尝试使用 rake 命令运行 rspec,以迁移数据库:rake rspec

于 2013-10-31T02:47:26.003 回答
0

我的问题是在模型中我创建了一个名为的私有方法:send(忘记它已经在 Ruby 中使用了)。

于 2015-02-06T12:38:50.720 回答