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

发布于 2024-12-18 15:38:20 字数 1427 浏览 1 评论 0原文

每当我创建一个 Factory 对象时,当工厂在该对象上设置属性时,我都会收到此错误 - 在本例中为“名称”。为什么???

businesss.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) 

Anytime I create a Factory object, I get this error where the factory sets a property on that object -- in this case 'name'. Why???

businesses.rb

FactoryGirl.define do
  factory :business do

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

businesses table

        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) 

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

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

发布评论

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

评论(1

夏末的微笑 2024-12-25 15:38:20

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

Run rake db:test:clone and then all test DB columns will be mapped properly to Active Record methods.

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