Rspec-rails: Rails 6.1.0-rc2 的未定义方法“assert_nothing_raised”

创建于 2020-12-07  ·  5评论  ·  资料来源: rspec/rspec-rails

你使用的是什么 Ruby、Rails 和 RSpec 版本?

红宝石版本:2.6.6
导轨版本:6.1.0-rc2
RSpec 版本:4.1.0.pre(分支 6-1-dev)

观察到的行为

运行 rspec 会引发错误:

NoMethodError:
       undefined method `assert_nothing_raised' for #<RSpec::ExampleGroups::Test "fails" (./spec/test_spec.rb:5)>

预期行为

不应引发“未定义方法”错误; 它一直工作到 Rails 6.0.3.4。

你能提供一个示例应用程序吗?

创建一个新应用并将其添加到 Gemfile:

%w[rspec rspec-core rspec-expectations rspec-mocks rspec-support].each do |lib|
  gem lib, git: "https://github.com/rspec/#{lib}.git", branch: "main"
end
git "https://github.com/rspec/rspec-rails", branch: "rails-6-1-dev" do
  gem "rspec-rails"
end

安装:

bundle install
rails generate rspec:install

添加测试:

# rspec/test_spec.rb

# frozen_string_literal: true
require "rails_helper"

RSpec.describe "test" do
  it "fails" do
    assert_nothing_raised()
  end
end

运行测试:

bundle exec rspec spec/test_spec.rb

最有用的评论

方法定义位于ActiveSupport::Testing::Assertions中。

因此,您还可以通过以下方式解决它:

RSpec.configure do |config|
  config.include ActiveSupport::Testing::Assertions
end

所有5条评论

我有同样的问题,因为使用ActiveJob::TestHelpers在下面使用该方法:

RSpec.describe do
  include ActiveJob::TestHelper
  it 'works' do
    perform_enqueued_jobs do
      SomeService.doSomething
    end
  end
end
       NoMethodError:
         undefined method `assert_nothing_raised' for #<RSpec::ExampleGroups::xxxx:0x0000559e7b2a3018>
         Did you mean?  assert_raises
       # .../gems/rspec-expectations-3.10.0/lib/rspec/matchers.rb:965:in `method_missing'
       # .../gems/rspec-core-3.10.0/lib/rspec/core/example_group.rb:764:in `method_missing'
       # .../gems/activejob-6.1.0.rc2/lib/active_job/test_helper.rb:591:in `perform_enqueued_jobs'

复制该功能的最佳方法是什么?

编辑

目前的解决方法:

RSpec.configure do |config|
   def assert_nothing_raised(&block)
      expect(&block).to_not raise_error
   end
end

听起来它定义的模块已经移动,我们引入了一堆断言模块来支持各种帮助程序。

方法定义位于ActiveSupport::Testing::Assertions中。

因此,您还可以通过以下方式解决它:

RSpec.configure do |config|
  config.include ActiveSupport::Testing::Assertions
end

@benoittgt你认为我们可以为 6.1 解决这个问题吗?

此页面是否有帮助?
0 / 5 - 0 等级