gpt4 book ai didi

ruby-on-rails - FactoryGirl:attributes_for没有给我相关的属性

转载 作者:行者123 更新时间:2023-12-03 08:56:25 24 4
gpt4 key购买 nike

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

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

但是当我用一个简单的test_should_create_code这样测试我的 Controller 时:
  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"}

有任何想法吗?

谢谢,

最佳答案

您可以尝试如下操作:

(Factory.build :code).attributes.symbolize_keys 

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

关于ruby-on-rails - FactoryGirl:attributes_for没有给我相关的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5103572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com