Rspec-rails: Rails6.1.0-rc2での未定義のメソッド「assert_nothing_raised」

作成日 2020年12月07日  ·  5コメント  ·  ソース: rspec/rspec-rails

使用しているRuby、Rails、RSpecのバージョンは何ですか?

Rubyバージョン:2.6.6
Railsバージョン: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)>

期待される動作

「未定義のメソッド」エラーは発生しません。 Rails6.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 評価