1

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。

4

2 回答 2

1

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

在重新启动我的 Spring 服务器之前,我能够使用设置属性,create :object, new_field: true但只是使用create :object不起作用,即使new_field true我的factories.rb文件中有。

于 2014-04-05T17:08:49.137 回答
0

Late to the party but I had the same problem. I had added all my attributes after the initial migration, and it turned out I was using attr_accessor instead of attr_accessible in my model.

于 2014-01-24T23:12:42.353 回答