Factory_girl 中的错误:NoMethodError:未定义的方法 - rspec 和 Rails 3.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在删除对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.