gpt4 book ai didi

ruby-on-rails - 失败/错误 : require 'rspec/rails' TypeError: wrong argument type Class (expected Module) Rspec V3

转载 作者:行者123 更新时间:2023-12-05 06:36:28 25 4
gpt4 key购买 nike

我知道,这个错误很常见,但不知何故我无法找出问题所在,似乎与它的路径有关。我关注了this也有问题,但到目前为止还没有运气。

gem 文件

group :development, :test do
gem 'faker'
gem 'rspec-rails', '~> 3.7'
gem 'factory_bot_rails', '~> 4.0'
gem 'shoulda-matchers', '~> 3.1'
end

spec/factories/user.rb

FactoryBot.define do
factory :user do
name "Andy"
end
end

spec/models/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

spec/support/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'
...
...

我正在运行 bundle exec rspec spec/models/user_spec.rb

我尝试过的事情:

  1. rails_helper.rb require 'rspec/rails' 行下方添加了 require 'support/factory_bot'
  2. rails generate rspec:install(执行成功)
  3. rspec --init(执行成功)
  4. rake db:test:prepare(执行成功)
  5. user_spec.rb 中将 require 'rails_helper' 更改为 require 'spec_helper' 但出现错误 NameError: uninitialized constant User 运行后。

这是完整的轨迹:

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

最佳答案

我成功了。

除了一件事,一切都在这个地方。class 替换了 module 我有一个 lib 和一个 helper 模块导致错误。只需将 module 替换为 class 现在它运行起来就像黄油一样。

关于ruby-on-rails - 失败/错误 : require 'rspec/rails' TypeError: wrong argument type Class (expected Module) Rspec V3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49089710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com