Rails FactoryBot 未初始化常量 Mentor

Rails FactoryBot uninitialized constant Mentor

我使用 Rails 5 和 ruby 2.4.1

我开始对应用程序进行 rspec 测试...

这是我的第一次测试,我没有成功

之后我会用Faker gem 来放置随机信息,但首先我需要这个 运行 成功,请帮助我!

spec_helper.rb

require 'factory_bot_rails'
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
  config.before(:all) do
    FactoryBot.reload
  end

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups
end

rails_helper.rb

require 'spec_helper'

ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../config/environment', __dir__)

abort("The Rails environment is running in production mode!") if Rails.env.production?

require 'rspec/rails'

begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
end

factories.rb

FactoryBot.define do

  factory :plan do
    name { "Plano Livre" }
    nickname { "plano_livre" }
    price { 1000 }
    modules { ["Dataminer","Events", "Jobs", "RoomReservation", "Mentoring", "News", "Visitors", "Packages", "RocketChat", "Education", "StartupReview", "CorporateChallenges", "News", "Benefits", "HubDigital", "DigitalHub"] }
  end

  factory :user do
    name { "Orville Gibson" }
    email { "orville_gibson@mailinator.com" }
    password { "Teste123" }
    password_confirmation { "Teste123" }
    role { "admin" }
    plan { association :plan }
  end

  factory :mentor do
    user { association :user }
    time_band_init { "8:00" }
    time_band_end { "18:00" }
    mini_bio { "Thomas Cruise Mapother IV é um ator e produtor de cinema norte-americano de origem irlandesa." }
    mentoring_types { ["online"] }
  end

end

mentor_controller_spec.rb

require 'spec_helper'


describe 'Mentoring::MentorController' do
  before do
    @mentor = build(:mentor)
  end

  context 'GET dashboard' do
    it 'returns a 200' do
      get :mentor_dashboard_path
      expect(response).to have_http_status(:ok)
    end
  end

end

编辑 1: app/models/

编辑 2: app/models/mentor.rb(文件开头)

class Mentor < ApplicationRecord

编辑 3: Mentor.new

编辑4: Mentor.new env=test

当我在控制台上 运行 rspec 时出现此错误:

 1) Mentoring::MentorController GET dashboard returns a 200
 Failure/Error: @mentor = build(:mentor)
 
 NameError:
   uninitialized constant Mentor
   Did you mean?  Monitor
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/activesupport-5.2.4.3/lib/active_support/inflector/methods.rb:283:in `block in constantize'
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/activesupport-5.2.4.3/lib/active_support/inflector/methods.rb:281:in `each'
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/activesupport-5.2.4.3/lib/active_support/inflector/methods.rb:281:in `inject'
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/activesupport-5.2.4.3/lib/active_support/inflector/methods.rb:281:in `constantize'
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/activesupport-5.2.4.3/lib/active_support/core_ext/string/inflections.rb:68:in `constantize'
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/factory_bot-5.2.0/lib/factory_bot/factory.rb:26:in `build_class'
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/factory_bot-5.2.0/lib/factory_bot/factory.rb:37:in `run'
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/factory_bot-5.2.0/lib/factory_bot/factory_runner.rb:29:in `block in run'
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/activesupport-5.2.4.3/lib/active_support/notifications.rb:170:in `instrument'
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/factory_bot-5.2.0/lib/factory_bot/factory_runner.rb:28:in `run'
 # /home/jhonny/.rvm/gems/ruby-2.4.1@distrito/gems/factory_bot-5.2.0/lib/factory_bot/strategy_syntax_method_registrar.rb:28:in `block in define_singular_strategy_method'
 # ./spec/controllers/mentoring/mentor_controller_spec.rb:6:in `block (2 levels) in <top (required)>'

你应该 require 'rails_helper' 在你的 rspec 测试中,而不是 spec_helper。

rails_helper 将为 rspec 设置 rails 环境并加载 spec_helper,但 spec_helper 不会加载 rails_helper。没有rails_helper,rspec不知道如何加载Rails代码。