Marcelo Boeira
Marcelo Boeira

Reputation: 880

Rspec + FactoryGirl + Faker tests failing randomly

I have a study-case project, working with Rails 4, RSpec, FactoryGirl, Faker...

But rspec tests are failing randomly, on development env. I'am searching a lot, but if anyone could help me figure it out, I would appreciated.

Here are the links to the test file, factory and the model:

When it fails, produce always a stack almost like this:

 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)>'

Upvotes: 0

Views: 636

Answers (2)

Marcelo Boeira
Marcelo Boeira

Reputation: 880

The problems was Faker, it doesn't provide a control for email and username values. Anyway Ecnalyr helped a lot on finding the problem :)

Upvotes: 0

Ecnalyr
Ecnalyr

Reputation: 5802

You changed the locale to something other than the rails default and are getting errors that don't have a translation into that locale.

An easy work-around would be to temporarily change the locale back to the default so you are more likely to get the actual error message (Looks like activerecord is trying to tell you something but it doesn't know how to say it in pt-BR). It will tell you this error in English, but it seems like you know English so that shouldn't be a problem.

Fix the error, then switch the locale back to your desired locale and the error shouldn't appear any more.

If you need to avoid changing the locale you will need to add a translation appropriate to your chosen locale for the specific error. There are sources elsewhere that can help with this process. Here are some links with related information:

Update: A shot in the dark, but your specific error may have to do specifically with FactoryGirl having issues during initialization. . . This answer looks like it may work with your problem: https://stackoverflow.com/a/11644746/1026898.

Upvotes: 1

Related Questions