【问题标题】:Keep getting record_invalid while using FactoryGirl使用 FactoryGirl 时不断收到 record_invalid
【发布时间】:2012-07-25 15:31:19
【问题描述】:

很多时候,当我在我的规范中使用 FactoryGirl 时,我不断收到如下错误:

Failure/Error: f = FactoryGirl.build(:my_model)
     ActiveRecord::RecordInvalid:
       translation missing: de.activerecord.errors.messages.record_invalid

现在,如果我在测试模式下启动控制台,我会返回一条记录,并且不会出现任何错误。 我应该如何处理?

编辑:我错过了翻译并且没有看到相关消息。在我切换到:en 语言环境后,错误就很明显了。

【问题讨论】:

  • 验证失败,因为一些关联没有建立。
  • @Geo 你能指定解决方案吗?我也有同样的问题!
  • 对不起。我现在看到你的编辑评论了。

标签: ruby-on-rails ruby


【解决方案1】:

可能您无法构建模型,因为它在初始化期间需要一些参数。使用initialize_with

factory :user do
  name "John Doe"

  initialize_with { new(name) }
end

查看https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#custom-construction了解详情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-02
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多