factory_girl不设置模型属性

时间:2013-10-17 18:42:33

标签: ruby-on-rails ruby factory-bot

factory_girl没有正确设置模型上的url属性。我看着是否有保留字,但我一无所获。

  factory :attachment do
    association :attachable, factory: :upload

    url "some/path"
    description "Some important file"
  end

我失败了。请注意url: nil

1) Attachment should be valid
     Failure/Error: it { should be_valid }
       expected #<Attachment id: 1048, attachable_id: 1047, attachable_type: "Upload", name: nil, url: nil, created_at: nil, updated_at: nil, description: "Some important file"> to be valid, but got errors: Url can't be empty
     # ./spec/models/attachment_spec.rb:14:in `block (2 levels) in <top (required)>'

我在控制台中运行了factory_girl,并使用FactorGirl.attributes_for :attachment

获得了相同的结果

使用FactoryGirl 4.2.1运行Rails 4。

2 个答案:

答案 0 :(得分:0)

晚会但我遇到了同样的问题。我在初始迁移后添加了所有属性,结果发现我在模型中使用的是attr_accessor而不是attr_accessible

答案 1 :(得分:0)

我遇到了同样的问题,在桌子上敲了几个小时后,我决定停下来重启我的Spring服务器。它开始像魔术一样工作。

在重新启动Spring服务器之前,我可以使用create :object, new_field: true设置属性,但只使用create :object就行不通,即使我new_field true中有factories.rb文件。

相关问题