gpt4 book ai didi

ruby-on-rails - Nil :NilClass 的 Factory Girl 未定义方法

转载 作者:数据小太阳 更新时间:2023-10-29 07:19:51 26 4
gpt4 key购买 nike

我有一个类似这样的 Controller 规范

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

最佳答案

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

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

关于ruby-on-rails - Nil :NilClass 的 Factory Girl 未定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19697045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com