FactoryGirl:attributes_for没有给我相关的属性 [英] FactoryGirl: attributes_for not giving me associated attributes

查看:42
本文介绍了FactoryGirl:attributes_for没有给我相关的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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"}

有什么想法吗?

谢谢

推荐答案

您可以尝试执行以下操作:

You can try something like this:

(Factory.build :code).attributes.symbolize_keys 

检查此内容: http://groups.google.com/group/factory_girl/browse_thread/thread/a95071d66d97987e )

这篇关于FactoryGirl:attributes_for没有给我相关的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆