FactoryGirl RSpec在工厂设置该对象的属性时创建工厂对象时没有方法错误

时间:2011-11-29 19:35:31

标签: ruby-on-rails rspec factory-bot

无论何时我创建一个Factory对象,我都会在工厂设置该对象的属性时出现此错误 - 在这种情况下' name'。为什么???

businesses.rb

FactoryGirl.define do
  factory :business do

    factory :business_main do
      name "Dave's Toys"
    end
  end
end

企业表

        Column        |            Type             |                        Modifiers                        
----------------------+-----------------------------+---------------------------------------------------------
 id                   | integer                     | not null default nextval('businesses_id_seq'::regclass)
 name                 | character varying(255)      | 
 address_line1        | character varying(255)      | 
 address_line2        | character varying(255)      | 
 city                 | character varying(255)      | 
 state                | character varying(255)      | 
 zip                  | character varying(255)      | 

business_spec.rb

...
business  = Factory(:business_main)
...

 # Failure/Error: business  = Factory(:business_main)
 # NoMethodError:       undefined method `name=' for #<Business:0x007fc991684590>

-

Using rails (3.1.3) 
Using factory_girl (2.3.2) 
Using factory_girl_rails (1.4.0)
Using rspec-core (2.7.1) 
Using rspec-expectations (2.7.0) 
Using rspec-mocks (2.7.0) 
Using rspec (2.7.0) 
Using rspec-rails (2.7.0) 

1 个答案:

答案 0 :(得分:7)

运行rake db:test:clone,然后所有测试数据库列将正确映射到Active Record方法。