据我所知,这个错误很常见,但不知怎的,我无法弄清楚问题,似乎与它的路径有关。 我也跟着this问题但到目前为止没有运气。
的Gemfile
group :development, :test do
gem 'faker'
gem 'rspec-rails', '~> 3.7'
gem 'factory_bot_rails', '~> 4.0'
gem 'shoulda-matchers', '~> 3.1'
end
规格/工厂/ user.rb
FactoryBot.define do
factory :user do
name "Andy"
end
end
规格/模型/ user_spec.rb
require 'rails_helper'
RSpec.describe User, type: :model do
it "should be invalid without phone number" do
user = build(:user)
user.phone = nil
expect(user).not_to be_valid
end
end
规格/支持/ factory_bot.rb
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
.rspec
--require spec_helper
--color
rails_helper.rb
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'support/factory_bot'
...
...
我正在运行捆绑exec rspec spec / models / user_spec.rb
我尝试的事情:
require 'support/factory_bot'
行以下的rails_helper.rb
添加了require 'rspec/rails'
。rails generate rspec:install
(已成功执行)require 'rails_helper'
更改为require 'spec_helper'
,但在运行后收到错误 NameError:未初始化的常量用户。以下是完整的跟踪:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: require 'rspec/rails'
TypeError:
wrong argument type Class (expected Module)
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:181:in `include'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:181:in `block in add_template_helper'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:181:in `module_eval'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:181:in `add_template_helper'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:110:in `block in helper'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:109:in `each'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/abstract_controller/helpers.rb:109:in `helper'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_controller/railties/helpers.rb:17:in `inherited'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionview-5.0.6/lib/action_view/test_case.rb:11:in `<class:TestCase>'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionview-5.0.6/lib/action_view/test_case.rb:10:in `<module:ActionView>'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/actionview-5.0.6/lib/action_view/test_case.rb:8:in `<top (required)>'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `block in require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:259:in `load_dependency'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `block in require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:259:in `load_dependency'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `block in require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:259:in `load_dependency'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `block in require'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:259:in `load_dependency'
# /Users/Imi/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in `require'
# ./spec/rails_helper.rb:7:in `<top (required)>'
# ./spec/models/user_spec.rb:1:in `require'
# ./spec/models/user_spec.rb:1:in `<top (required)>'
No examples found.
Finished in 0.00789 seconds (files took 2.75 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
答案 0 :(得分:0)
我知道了。
除了一件东西,所有东西都在那儿。
将module
替换为class
。我有一个lib
和一个helper
模块引起了错误。只需将module
替换为class
,现在它就像黄油一样运转。