Rspec + FactoryGirl + Faker测试随机失败

时间:2015-01-16 13:17:57

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

我有一个研究案例项目,与Rails 4,RSpec,FactoryGirl,Faker一起工作......

但是在开发环境中,rspec测试随机失败。我经常搜索,但是如果有人能帮我搞清楚,我会很感激。

以下是测试文件,工厂和模型的链接:

当它失败时,总是产生一个像这样的堆栈:

 Failure/Error: @chloe = FactoryGirl.create(:user)
 ActiveRecord::RecordInvalid:
   translation missing: pt-BR.activerecord.errors.messages.record_invalid
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.1.8/lib/active_record/validations.rb:57:in `save!'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.1.8/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:273:in `block in save!'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:208:in `transaction'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.1.8/lib/active_record/transactions.rb:273:in `save!'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `[]'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `create'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `tap'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.1.8/lib/active_support/notifications.rb:161:in `instrument'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
 # /Users/marceloboeira/.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
 # ./spec/models/user_spec.rb:8:in `block (2 levels) in <top (required)>'

2 个答案:

答案 0 :(得分:1)

您已将区域设置更改为rails默认值以外的其他内容,并且收到的错误无法转换为该区域设置。

一个简单的解决方法是暂时将区域设置更改回默认设置,这样您就更有可能收到实际的错误消息(看起来activerecord试图告诉您一些事情,但事实并非如此。我知道如何在pt-BR中说出来。它会用英语告诉你这个错误,但似乎你懂英语,所以不应该成为一个问题。

修正错误,然后将区域设置切换回所需的区域设置,错误不应再出现。

如果您需要避免更改区域设置,则需要为特定错误添加适合您所选语言环境的翻译。其他地方的消息来源可以帮助完成这一过程。以下是一些相关信息的链接:

<强>更新 在黑暗中拍摄,但您的具体错误可能与FactoryGirl在初始化期间出现问题有关。 。 。这个答案看起来似乎可能处理您的问题:https://stackoverflow.com/a/11644746/1026898

答案 1 :(得分:0)

问题是Faker,它不提供对电子邮件和用户名值的控制。无论如何,Ecnalyr在找到问题方面做了很多帮助:)