【问题标题】:Rspec Failure - NameError: uninitialized constant FakerRspec 失败 - NameError: 未初始化的常量 Faker
【发布时间】:2016-09-19 06:45:29
【问题描述】:

我正面临这种失败:在 Rspec 中 - 使用 Mongodb 作为数据库并设计用于用户管理。无法理解是什么问题。

用这个命令

bundle exec rspec spec/models/user_spec.rb

      FFFF

Failures:

  1) User 
     Failure/Error: email { Faker::Internet.email }

     NameError:
       uninitialized constant Faker
     # ./spec/factories/users.rb:3:in `block (3 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:19:in `block (2 levels) in <top (required)>'

  2) User 
     Failure/Error: email { Faker::Internet.email }

     NameError:
       uninitialized constant Faker
     # ./spec/factories/users.rb:3:in `block (3 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:19:in `block (2 levels) in <top (required)>'

  3) User 
     Failure/Error: email { Faker::Internet.email }

     NameError:
       uninitialized constant Faker
     # ./spec/factories/users.rb:3:in `block (3 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:19:in `block (2 levels) in <top (required)>'

  4) User 
     Failure/Error: email { Faker::Internet.email }

     NameError:
       uninitialized constant Faker
     # ./spec/factories/users.rb:3:in `block (3 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:19:in `block (2 levels) in <top (required)>'

Finished in 0.00478 seconds (files took 2.1 seconds to load)
4 examples, 4 failures

Failed examples:

rspec ./spec/models/user_spec.rb:23 # User 
rspec ./spec/models/user_spec.rb:24 # User 
rspec ./spec/models/user_spec.rb:25 # User 
rspec ./spec/models/user_spec.rb:26 # User 

/spec/models/user_spec.rb

require 'spec_helper'


describe User do
    before { @user = FactoryGirl.build(:user)}

    subject { @user }

    it { should respond_to(:email) }
    it { should respond_to(:password) }
    it { should respond_to(:password_confirmation) }
    it { should be_valid }
end

spec/factories/users.rb

FactoryGirl.define do
  factory :user do
    email { Faker::Internet.email }
    password "12345678"
    password_confirmation "12345678"
  end
end

宝石文件

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
# gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

gem 'mongoid', '~> 5.1.0'

#Api gems
gem 'active_model_serializers'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  gem "factory_girl_rails"
  gem 'ffaker'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

group :test do
  gem "rspec-rails"
end

gem "devise"

【问题讨论】:

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


    【解决方案1】:

    NameError: 未初始化的常量 Faker

    您的应用程序中有ffaker gem,它是faker重构版本。所以你需要使用FFaker 而不是Faker

    email { FFaker::Internet.email }
    

    【讨论】:

    • 你知道这个Faker什么时候被弃用的吗?
    猜你喜欢
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多