相关疑难解决方法(0)

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

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

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

但是,当我用一个简单的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
Run Code Online (Sandbox Code Playgroud)

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

在控制台中,似乎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"}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

谢谢,

ruby-on-rails ruby-on-rails-3 factory-bot

31
推荐指数
3
解决办法
1万
查看次数