I18n::MissingTranslationData: translation missing: en.faker error when seeding db - ruby-on-rails

I want to seed the database with Faker, the problem is that I am getting an error when I do a:
rake db:reset
I get this message:
rake aborted!
I18n::MissingTranslationData: translation missing: en.faker.name.name
/Library/Ruby/Gems/2.0.0/gems/i18n-0.7.0/lib/i18n.rb:311:in `handle_exception'
/Library/Ruby/Gems/2.0.0/gems/i18n-0.7.0/lib/i18n.rb:161:in `translate'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:128:in `rescue in translate'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:120:in `translate'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:86:in `fetch'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:99:in `parse'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker/name.rb:8:in `name'
/Users/hbendev/code/wikitec/db/seeds.rb:6:in `block in <top (required)>'
/Users/hbendev/code/wikitec/db/seeds.rb:4:in `times'
/Users/hbendev/code/wikitec/db/seeds.rb:4:in `<top (required)>'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/Library/Ruby/Gems/2.0.0/gems/railties-4.2.0/lib/rails/engine.rb:547:in `load_seed'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:139:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:setup => db:seed
I don't know why is that error appearing, since I haven't got any problems with Faker before, I just wanted to reset the db to update the seeds.
I google it but I can't find anything related that solves the problem.
I tried to add:
I18n.reload!
After require 'faker' in my seeds.rb file, but no luck.
Looks like the problem is with Faker itself, because the database is being created properly, when I do a rake db:drop db:create db:migrate it works, until there, but when I try to seed the database with Faker with rake db:seed or rake db:reset, I get the error.
What can I do? Thanks in advance.
UPDATE - I included seeds.rb and en.yml files
seeds.rb:
require 'faker'
# Create Users
5.times do
user = User.new(
name: Faker::Name.name,
email: Faker::Internet.email,
password: Faker::Lorem.characters(10)
)
user.skip_confirmation!
user.save!
end
users = User.all
# Create Wikis
25.times do
Wiki.create!(
title: Faker::Lorem.sentence,
body: Faker::Lorem.paragraph,
:private => false,
user: users.sample
)
end
# Create Admin account
admin = User.new(
name: 'Admin User',
email: 'admin#example.com',
password: 'helloworld',
role: 'admin'
)
admin.skip_confirmation!
admin.save!
# Create Premium account
premium = User.new(
name: 'Premium User',
email: 'premium#example.com',
password: 'helloworld',
role: 'premium'
)
premium.skip_confirmation!
premium.save!
# Create Standard account
standard = User.new(
name: 'Standard User',
email: 'standard#example.com',
password: 'helloworld',
role: 'standard'
)
standard.skip_confirmation!
standard.save!
puts "Seed finished"
puts "#{Wiki.count} wikis created"
puts "#{User.count} users created"
en.yml:
en:
hello: "Hello world"

In my case, the I18n available_locales config did not include en:
config.i18n.available_locales = %i[de de_en]
I reverted it back to
config.i18n.available_locales = %i[de en]
and it worked.

Check the I18n Faker configuration info here:
https://github.com/stympy/faker#customization
Looks like you should enforce the I18n Faker locale in case you are using a non standard locale in your app.
Just set Faker::Config.locale to the locale you want, and Faker will
take care of the rest.

This worked for me...
In your Gemfile add :require => false
group :development, :test do
#gem 'faker', '~> 1.4.3'
gem 'faker', :require => false
end
Add require "faker" manually...

I had the same problem.
it is necessary to move faker to Gemfile from
group: development: test do
gem 'faker'
end
for me it solved a problem
good luck

Related

cannot load such file -- capybara/minitest

hope somebody can help me with this. I did search but haven't found any working solution.
I've started writing test for an app. My integration tests run fine, but then I decided that since I'm not that much of TDD driven and since I don't have that much time right now to extensively test all layers of the app that I should use instead of integration tests system tests, because they allow me to test the full flow as if in a browser.
Rails 5.1.2
Gemfile
(tried different variations, just capybara, then with combinations of both the other two)
gem 'minitest-rails'
gem 'minitest-rails-capybara'
gem 'capybara'
test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
EMPTY_NEW_USER = {
email: '',
first_name: '',
last_name: '',
username: '',
password: ''
}
EXISTING_USER = {
email: '****',
first_name: 'John',
last_name: 'Doe',
username: '',
password: 'testingpass',
password_confirmation: 'testingpass'
}
# Add more helper methods to be used by all tests here...
end
application_system_test_case.rb
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
end
register_logins.rb
require "application_system_test_case"
class RegisterLoginsTest < ApplicationSystemTestCase
test 'full login flow' do
visit root_url
assert_response :success
find('.email_link').click
end
end
error when running
rake test:system
LoadError: cannot load such file -- capybara/minitest
/Users/mnussbaumer/code/dvouch/test/application_system_test_case.rb:3:in `<top (required)>'
/Users/mnussbaumer/code/dvouch/test/system/register_logins_test.rb:1:in `<top (required)>'
Tasks: TOP => test:system
(See full trace by running task with --trace)
The full trace adds this:
LoadError: cannot load such file -- capybara/minitest
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `block in require'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.1.2/lib/action_dispatch/system_test_case.rb:2:in `<top (required)>'
/Users/mnussbaumer/code/dvouch/test/application_system_test_case.rb:3:in `<top (required)>'
and goes on on active_support dependencies.
What I have tried:
Adding one, two and three to test_helper.rb:
require "capybara/rails"
require "minitest/rails"
require "minitest/rails/capybara"
I tried with gems:
group :development, :test do
gem 'minitest-rails'
gem 'minitest-capybara'
gem 'capybara'
end
then with 'minitest-rails-capybara'
Thanks
The file capybara/minitest was added to Capybara in version 2.13.0, which is the minimum version Rails requires for its system tests since Rails 5.1.0. Upgrade to the latest version of Capybara (2.14.4) and there should be no need for the minitest-capybara or minitest-rails gems. You will need to also add the 'selenium-webdriver' gem to your test group.
Additionally the assert_response :success line is't valid in Capybara tests because the HTTP response code from the browser Capybara is using isn't generally available.

Uninitialized constant FAKER when "seeding" db [duplicate]

I am trying to run a simple bundle exec rake db:seed for my database in Rails 4. However, when running it, I get the following output:
********-C02MGBVJFD57:myapp ***********$ bundle exec rake db:seed
Your Gemfile lists the gem factory_girl_rails (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
rake aborted!
NameError: uninitialized constant Faker
/Users/**********/workspace/myapp/db/seeds.rb:16:in `block in <top (required)>'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `times'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `<top (required)>'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/railties-4.1.4/lib/rails/engine.rb:543:in `load_seed'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:184:in `load_seed'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
Here is my seeds.rb file:
User.create!(
name: "Example User",
email: "example#railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true
)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(
name: name,
email: email,
password: password,
password_confirmation: password
)
end
Line 16 is:
name = Faker::Name.name
Any ideas why I am getting this error? Thank you.
Just faced similar issue - I was running
rails g model model_name
and getting the error:
uninitialized constant Faker (NameError)
Problem was due to fact, that I had gem added to test group.
Placing it into development and test group solved the problem:
group :development, :test do
# ...
gem 'faker'
# ...
end
I faced the same issue while writing rspec and adding require 'faker' in spec file solved it.
I added gem 'faker' in the Gemfile. Then I run bundle install to get the gem.
As per official faker documentation, it said:
Note: if you are getting a uninitialized constant Faker::[some_class]
error, your version of the gem is behind the one documented here. To
make sure that your gem is the one documented here, change the line in
your Gemfile to:
gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main'
But the problem still persist with mine app, since I have done this and I am getting the error again, but only when try to run the migrations to heroku:
heroku run rails db:migrate db:seed
When I run the command locally I do not have the problem and migrations and seeds are executed.

NameError: uninitialized constant Faker

I am trying to run a simple bundle exec rake db:seed for my database in Rails 4. However, when running it, I get the following output:
********-C02MGBVJFD57:myapp ***********$ bundle exec rake db:seed
Your Gemfile lists the gem factory_girl_rails (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
rake aborted!
NameError: uninitialized constant Faker
/Users/**********/workspace/myapp/db/seeds.rb:16:in `block in <top (required)>'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `times'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `<top (required)>'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/railties-4.1.4/lib/rails/engine.rb:543:in `load_seed'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:184:in `load_seed'
/Users/**********/.rvm/gems/ruby-2.1.2#myapp/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
Here is my seeds.rb file:
User.create!(
name: "Example User",
email: "example#railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true
)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(
name: name,
email: email,
password: password,
password_confirmation: password
)
end
Line 16 is:
name = Faker::Name.name
Any ideas why I am getting this error? Thank you.
Just faced similar issue - I was running
rails g model model_name
and getting the error:
uninitialized constant Faker (NameError)
Problem was due to fact, that I had gem added to test group.
Placing it into development and test group solved the problem:
group :development, :test do
# ...
gem 'faker'
# ...
end
I faced the same issue while writing rspec and adding require 'faker' in spec file solved it.
I added gem 'faker' in the Gemfile. Then I run bundle install to get the gem.
As per official faker documentation, it said:
Note: if you are getting a uninitialized constant Faker::[some_class]
error, your version of the gem is behind the one documented here. To
make sure that your gem is the one documented here, change the line in
your Gemfile to:
gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main'
But the problem still persist with mine app, since I have done this and I am getting the error again, but only when try to run the migrations to heroku:
heroku run rails db:migrate db:seed
When I run the command locally I do not have the problem and migrations and seeds are executed.

Run cucumber through spork

I have the Exception encountered: #<SystemExit: exit> error, when trying run cucumber through spork.
features/support/env.rb:
require 'rubygems'
require 'spork'
require 'cucumber/rails'
require 'pickle/world'
require "capybara/poltergeist"
Spork.prefork do
Capybara.javascript_driver = :poltergeist
end
Spork.each_run do
ActionController::Base.allow_rescue = false
begin
DatabaseCleaner[:mongoid].strategy = :truncation
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
DatabaseCleaner[:mongoid].clean
Cucumber::Rails::Database.javascript_strategy = :truncation
end
Run without spork:
vagrant#lucid32:/vagrant$ bundle exec cucumber
WARNING: No DRb server is running. Running features locally:
Rack::File headers parameter replaces cache_control after Rack 1.5.
Using the default profile...
Feature: Password recovery
In order to retrieve lost password
As a user of this site
I want to reset it
Scenario: Reset password # features/users/password_recovery.feature:6
Given I am not logged in # features/step_definitions/login_steps.rb:1
And a user exists # features/step_definitions/pickle_steps.rb:4
1 scenario (1 passed)
2 steps (2 passed)
0m0.504s
Run with spork:
vagrant#lucid32:/vagrant$ bundle exec cucumber
Disabling profiles...
Feature: Password recovery
In order to retrieve lost password
As a user of this site
I want to reset it
Scenario: Reset password # features/users/password_recovery.feature:6
Given I am not logged in # features/step_definitions/login_steps.rb:1
And a user exists # features/step_definitions/pickle_steps.rb:4
1 scenario (1 passed)
2 steps (2 passed)
0m0.495s
Exception encountered: #<SystemExit: exit>
backtrace:
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/cucumber-1.3.1/lib/cucumber/cli/main.rb:54:in `exit'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/cucumber-1.3.1/lib/cucumber/cli/main.rb:54:in `rescue in execute!'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/cucumber-1.3.1/lib/cucumber/cli/main.rb:37:in `execute!'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/test_framework/cucumber.rb:24:in `run_tests'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:13:in `block in run'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:21:in `block in initialize'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:18:in `fork'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:18:in `initialize'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `new'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `run'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/server.rb:48:in `run'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/drb/drb.rb:1548:in `perform_without_block'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/drb/drb.rb:1508:in `perform'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/drb/drb.rb:1586:in `block (2 levels) in main_loop'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/drb/drb.rb:1582:in `loop'
/usr/local/rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/drb/drb.rb:1582:in `block in main_loop'
How to solve it?
I also encountered this. Not sure why, but I guess this is because I'd updated cucumber-rails to the latest version. After I switched back to older version, the problem's gone
try putting these lines into your Gemfile
gem 'cucumber', '1.2.5'
gem 'cucumber-rails', '1.3.0', :require => false
then bundle install
I hope this helps
I submitted a pull request to spork to fix the issue.
https://github.com/sporkrb/spork/pull/228

rails3 - uninitialized constant in factory definition

factory_girl is not recognizing a model name that I've defined, and which I need to reference because I need a subclass. This worked in rails 2 but I'm migrating to rails 3.
Factory definition:
Factory.define :interest, :class => Term::Interest do |f|
f.name {"#{Factory.next(:lipsum_word)}ing"}
end
Definition of Term and Term::Interest
class Term < ActiveRecord::Base
belongs_to :category
class Interest < Term
end
class Award < Term
end
end
Error and stack trace:
$ rake db:data:load --trace
(in /Users/glurban/code/recruitd)
rake aborted!
uninitialized constant Term
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rspec-core-2.4.0/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
/Users/glurban/code/recruitd/test/factories/factories.rb:316:in `<top (required)>'
/Users/glurban/code/recruitd/lib/tasks/use_factories.rake:2:in `<top (required)>'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0/lib/rails/engine.rb:131:in `block in load_tasks'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0/lib/rails/engine.rb:131:in `each'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0/lib/rails/engine.rb:131:in `load_tasks'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0/lib/rails/application.rb:141:in `load_tasks'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0/lib/rails/application.rb:77:in `method_missing'
/Users/glurban/code/recruitd/Rakefile:7:in `<top (required)>'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2383:in `load'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2383:in `raw_load_rakefile'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2017:in `block in load_rakefile'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2016:in `load_rakefile'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2000:in `block in run'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/bin/rake:31:in `<top (required)>'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/bin/rake:19:in `load'
/Users/glurban/.rvm/gems/ruby-1.9.2-rc2/bin/rake:19:in `<main>'
I tried adding require_dependency 'term' to the top of factories.rb but then I get
Glens-MacBook-Pro:test glurban$ rake db:data:load
(in /Users/glurban/code/recruitd)
rake aborted!
No such file to load -- term
What to do?
Edit: in response to the comment, yes, it happens only on rake, not in the console:
$ rails c
Loading development environment (Rails 3.0.0)
ruby-1.9.2-rc2 > Factory(:term)
=> #<Term id: 3, type: nil, name: "Proud to be a Recruitd user", location: nil, category_id: nil, description: nil, url: nil, created_at: "2011-01-06 21:30:14", updated_at: "2011-01-06 21:30:14">
ruby-1.9.2-rc2 > Factory(:interest)
=> #<Term::Interest id: 4, type: "Term::Interest", name: "siting", location: nil, category_id: nil, description: nil, url: nil, created_at: "2011-01-06 21:30:18", updated_at: "2011-01-06 21:30:18">
Custom rake task definition:
require 'factory_girl'
require File.expand_path("test/factories/factories.rb")
namespace :db do
namespace :data do
desc "Load sample data"
task :load => :environment do |t|
create_students
...
create_student_files_and_feeds
puts "Completed loading sample data."
end
end
end
def create_interests
data_fetch("interests").each do |input|
Factory(:interest, :name => input.strip)
end
puts "Created interests"
end
After looking over the factory_girl documentation again, I realized that you can specify a class using either a string or a class constant. So I tried using a string and it worked:
Factory.define :interest, :class => "Term::Interest" do |f|
f.name {"#{Factory.next(:lipsum_word)}ing"}
end
Also, a comment about the nested classes. I initially did that to keep the classes a little more organized, but given the complexity it's created (especially for routing), I'm just going to move each subclass to its own model file. (If I were to un-nest them without putting them in separate files, rails seems to fail to find the subclasses sometimes--namely if the parent class hadn't been referenced (forcing the load of that file) before.

Resources