【问题标题】:Error in Factory_girl: NoMethodError: undefined method - rspec and rails 3.1.0Factory_girl 中的错误:NoMethodError:未定义的方法 - rspec 和 rails 3.1.0
【发布时间】:2011-11-14 10:37:35
【问题描述】:

我遇到了 factory_girl 的问题。这是 rspec 的错误。

“失败/错误:客户=工厂(:客户,:名称=>无) 无方法错误: #Customer:0x4175418 的未定义方法 `category1_id='

这是 rspec 代码:

describe "data integrity" do
  it "should return error with no name" do
    customer = Factory(:customer, :name => nil)
    customer.errors[:name].should_not be_empty
    customer.should_not be_valid
  end
  it "should take a good name" do
    customer = Factory(:customer, :name => "good customer name")
    customer.errors[:name].should be_empty
  end
end

category1_id 是客户表中的一列。这是customer.rb

class Customer < ActiveRecord::Base
  validates :name, :presence => true
end

Factory(:customer)的定义

Factory.define :customer do |c|
  c.name "test customer"
  c.email "t@acom.com"
  c.phone "12345678"
  c.cell "1234567890"
  c.active 1
  c.category1_id 2
  c.short_name "test" 
end

有什么想法吗?谢谢。

【问题讨论】:

  • 检查category1_id 列是否存在于您的customer 表中

标签: ruby-on-rails-3 rspec2 factory-bot


【解决方案1】:

在对 factory_girl_rails(由 factory_girl)的依赖被删除并且 Factory() 被 Factory.build() 替换后,问题就消失了。

不知何故 factory_girl 在我的系统中产生了对 factory_girl_rails 的依赖(仍然不知道为什么)。安装 factory_girl 后,gem check 需要安装 factory_girl_rails。但是,当 factory_girl 和 factory_girl_rails 都安装时,它们会产生冲突并出现重复错误。经过数小时的失败尝试,冲突依赖在重新启动和重新加载系统后神秘地消失了。

【讨论】:

  • 我遇到了同样的问题,还有一个后缀为 _id 的属性。作为最后的手段,我试图像你说的那样重新启动,但没有太大帮助。有趣的是它在 Rails 控制台中很好地创建了工厂,但在规范中没有。
猜你喜欢
  • 2017-12-09
  • 2017-01-13
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
相关资源
最近更新 更多