【问题标题】:Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller失败/错误:config.include ::Rails::Controller::Testing::TemplateAssertions,类型::controller
【发布时间】:2021-01-28 21:40:29
【问题描述】:

我正在尝试运行单个测试文件:bundle exec rspec spec/models/user_spec.rb

但得到以下错误:

    An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller

NameError:
  uninitialized constant Rails::Controller
  Did you mean?  ApiController
# ./spec/rails_helper.rb:149:in `block in <top (required)>'
# ./spec/rails_helper.rb:61:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.

最初,我收到以下错误:

An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: module Shoulda::Matchers::ActiveModel

NameError:
  uninitialized constant Shoulda
# ./spec/support/matchers/validate_kept_of_matcher.rb:4:in `<top (required)>'
# ./spec/rails_helper.rb:51:in `block in <top (required)>'
# ./spec/rails_helper.rb:51:in `each'
# ./spec/rails_helper.rb:51:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.

但是在我将以下内容添加到测试文件后它就修复了。

require "shoulda/matchers"

我是 Ruby/Rails 世界的新手,有人可以指点一下吗?

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    Rails::Controller::Testing::TemplateAssertionswas removed in Rails 5.

    您可以通过安装Rails controller testing gem 重新添加已弃用的功能。然而,RSpec 和 Rails 团队都不鼓励使用控制器规范、分配和模板断言,并且不是很有未来的证明。

    社区接受的解决方案是写 request specs 并停止在你的控制器中戳。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 2016-11-03
      相关资源
      最近更新 更多