FactoryGirl:attributes_for 没有给我关联的属性

发布于 2024-10-19 09:53:04 字数 974 浏览 7 评论 0原文

我有一个像这样的代码模型工厂:

Factory.define :code do |f|
    f.value "code"
    f.association :code_type
    f.association(:codeable, :factory => :portfolio)
end

但是当我使用像这样的简单 test_should_create_code 测试我的控制器时:

  test "should create code" do
    assert_difference('Code.count') do
      post :create, :code => Factory.attributes_for(:code)
    end
    assert_redirected_to code_path(assigns(:code))
  end

...测试失败。未创建新记录。

在控制台中,attributes_for 似乎没有像 create 那样返回所有必需的属性。

rob@compy:~/dev/my_rails_app$ rails console test
Loading test environment (Rails 3.0.3)
irb(main):001:0> Factory.create(:code)
=> #<Code id: 1, code_type_id: 1, value: "code", codeable_id: 1, codeable_type: "Portfolio", created_at: "2011-02-24 10:42:20", updated_at: "2011-02-24 10:42:20">
irb(main):002:0> Factory.attributes_for(:code)
=> {:value=>"code"}

有什么想法吗?

谢谢,

I have a Code model factory like this:

Factory.define :code do |f|
    f.value "code"
    f.association :code_type
    f.association(:codeable, :factory => :portfolio)
end

But when I test my controller with a simple test_should_create_code like this:

  test "should create code" do
    assert_difference('Code.count') do
      post :create, :code => Factory.attributes_for(:code)
    end
    assert_redirected_to code_path(assigns(:code))
  end

... the test fails. The new record is not created.

In the console, it seems that attributes_for does not return all required attributes like the create does.

rob@compy:~/dev/my_rails_app$ rails console test
Loading test environment (Rails 3.0.3)
irb(main):001:0> Factory.create(:code)
=> #<Code id: 1, code_type_id: 1, value: "code", codeable_id: 1, codeable_type: "Portfolio", created_at: "2011-02-24 10:42:20", updated_at: "2011-02-24 10:42:20">
irb(main):002:0> Factory.attributes_for(:code)
=> {:value=>"code"}

Any ideas?

Thanks,

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

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

发布评论

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

评论(7

陌上芳菲 2024-10-26 09:53:04

您可以尝试这样的操作:

(Factory.build :code).attributes.symbolize_keys 

检查以下内容:http://groups.google.com/group/factory_girl/ browser_thread/thread/a95071d66d97987e)

You can try something like this:

(Factory.build :code).attributes.symbolize_keys 

Check this: http://groups.google.com/group/factory_girl/browse_thread/thread/a95071d66d97987e)

寻梦旅人 2024-10-26 09:53:04

这个不返回时间戳等,只返回可进行批量分配的属性:

(FactoryGirl.build :position).attributes.symbolize_keys.reject { |key, value| !Position.attr_accessible[:default].collect { |attribute| attribute.to_sym }.include?(key) }

尽管如此,它还是很丑陋。我认为 FactoryGirl 应该提供类似这样的开箱即用的东西。

我在此处提出了对此的请求。

This one doesn't return timestamps etc., only attributes that are accessible for mass assignment:

(FactoryGirl.build :position).attributes.symbolize_keys.reject { |key, value| !Position.attr_accessible[:default].collect { |attribute| attribute.to_sym }.include?(key) }

Still, it's quite ugly. I think FactoryGirl should provide something like this out of the box.

I opened a request for this here.

萌︼了一个春 2024-10-26 09:53:04

我建议另一种方法,我认为它更清晰:

attr = attributes_for(:code).merge(code_type: create(:code_type))

I'd suggest yet an other approach, which I think is clearer:

attr = attributes_for(:code).merge(code_type: create(:code_type))
失与倦" 2024-10-26 09:53:04

这就是我最终要做的......

conf = FactoryGirl.build(:conference)
post :create, {:conference => conf.attributes.slice(*conf.class.accessible_attributes) }

heres what I end up doing...

conf = FactoryGirl.build(:conference)
post :create, {:conference => conf.attributes.slice(*conf.class.accessible_attributes) }
森罗 2024-10-26 09:53:04

我综合了一下其他人的说法,希望对其他人有帮助。为了与所讨论的 FactoryGirl 版本保持一致,我使用了 Factory.build() 而不是 FactoryGirl.build()。根据需要进行更新。

def build_attributes_for(*args)
  build_object = Factory.build(*args)
  build_object.attributes.slice(*build_object.class.accessible_attributes).symbolize_keys
end

只需调用此方法来代替 Factory.attributes_for:

post :create, :code => build_attributes_for(:code)

完整要点(在辅助模块内)位于:https: //gist.github.com/jlberglund/5207078

I've synthesized what others have said, in case it helps anyone else. To be consistent with the version of FactoryGirl in question, I've used Factory.build() instead of FactoryGirl.build(). Update as necessary.

def build_attributes_for(*args)
  build_object = Factory.build(*args)
  build_object.attributes.slice(*build_object.class.accessible_attributes).symbolize_keys
end

Simply call this method in place of Factory.attributes_for:

post :create, :code => build_attributes_for(:code)

The full gist (within a helper module) is here: https://gist.github.com/jlberglund/5207078

や莫失莫忘 2024-10-26 09:53:04

在我的 APP/spec/controllers/pages_controllers_spec.rb 中,我设置:

let(:valid_attributes) { FactoryGirl.attributes_for(:page).merge(subject: FactoryGirl.create(:theme), user: FactoryGirl.create(:user)) } 

因为我有两个关联的模型。这也有效:

 FactoryGirl.define do
    factory :page do
       title      { Faker::Lorem.characters 12 }
       body       { Faker::Lorem.characters 38 }
       discution  false
       published  true
       tags       "linux, education, elearning"
       section   { FactoryGirl.create(:section) }
       user      { FactoryGirl.create(:user)    }                                                                                                                            
     end
  end

In my APP/spec/controllers/pages_controllers_spec.rb I set:

let(:valid_attributes) { FactoryGirl.attributes_for(:page).merge(subject: FactoryGirl.create(:theme), user: FactoryGirl.create(:user)) } 

Because I have two models associated. This works too:

 FactoryGirl.define do
    factory :page do
       title      { Faker::Lorem.characters 12 }
       body       { Faker::Lorem.characters 38 }
       discution  false
       published  true
       tags       "linux, education, elearning"
       section   { FactoryGirl.create(:section) }
       user      { FactoryGirl.create(:user)    }                                                                                                                            
     end
  end
孤寂小茶 2024-10-26 09:53:04

这是另一种方法。您可能想省略 idcreated_atupdated_at 属性。

FactoryGirl.build(:car).attributes. except('id', 'created_at', 'updated_at').symbolize_keys

限制:

  • 它不会生成 HMT 和 HABTM 关联的属性(因为这些关联是存储在连接表中,而不是实际的属性)。
  • 工厂中的关联策略必须是create,如association :user,strategy: :create。如果你不明智地使用这个策略,它会让你的工厂变得非常缓慢。

Here's another way. You probably want to omit the id, created_at and updated_at attributes.

FactoryGirl.build(:car).attributes.except('id', 'created_at', 'updated_at').symbolize_keys

Limitations:

  • It does not generate attributes for HMT and HABTM associations (as these associations are stored in a join table, not an actual attribute).
  • Association strategy in the factory must be create, as in association :user, strategy: :create. This strategy can make your factory very slow if you don't use it wisely.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文