Factory_girl 中的错误:NoMethodError:未定义的方法 - rspec 和 Rails 3.1.0

发布于 2024-12-04 02:31:54 字数 1083 浏览 1 评论 0原文

我的工厂女孩​​有问题。这是 rspec 的错误。

“失败/错误:客户=工厂(:客户,:名称=> nil) 无方法错误: undefined method `category1_id=' for #Customer:0x4175418"

这是 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 表中的一列。这是 customer.rb

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

Factory(:customer) 的定义

Factory.define :customer do |c|
  c.name "test customer"
  c.email "[email protected]"
  c.phone "12345678"
  c.cell "1234567890"
  c.active 1
  c.category1_id 2
  c.short_name "test" 
end

有什么想法吗?谢谢。

I am having a problem with factory_girl. Here is the error with rspec.

"Failure/Error: customer = Factory(:customer, :name => nil)
NoMethodError:
undefined method `category1_id=' for #Customer:0x4175418"

Here is the rspec code:

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 is a column in customer table. Here is the customer.rb

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

The definition for Factory(:customer)

Factory.define :customer do |c|
  c.name "test customer"
  c.email "[email protected]"
  c.phone "12345678"
  c.cell "1234567890"
  c.active 1
  c.category1_id 2
  c.short_name "test" 
end

Any thoughts? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2024-12-11 02:31:54

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

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

The problem disappeared after the dependency on factory_girl_rails (by factory_girl) was dropped and Factory() was replaced with Factory.build().

Somehow factory_girl developed dependency on factory_girl_rails in my system (still don't know why). With factory_girl installed, gem check required factory_girl_rails to be installed. However when both factory_girl and factory_girl_rails installed, they generated conflict with duplicate error. After hours of failed tries, the conflict dependency mysteriously disappeared after rebooting and reloading the system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文