rspec / rspec-rails

RSpec for Rails 6+
https://rspec.info
MIT License
5.16k stars 1.03k forks source link

Undefined method "assert_nothing_raised" with Rails 6.1.0-rc2 #2410

Closed jenskutilek closed 3 years ago

jenskutilek commented 3 years ago

What Ruby, Rails and RSpec versions are you using?

Ruby version: 2.6.6 Rails version: 6.1.0-rc2 RSpec version: 4.1.0.pre (branch 6-1-dev)

Observed behaviour

Running rspec raises an error:

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

Expected behaviour

No "undefined method" error should be raised; it worked up until Rails 6.0.3.4.

Can you provide an example app?

Make a new app and add this to the 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

Install:

bundle install
rails generate rspec:install

Add a test:

# rspec/test_spec.rb

# frozen_string_literal: true
require "rails_helper"

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

Run the tests:

bundle exec rspec spec/test_spec.rb
pirj commented 3 years ago

Any reason not to use https://relishapp.com/rspec/rspec-expectations/v/3-9/docs/built-in-matchers/raise-error-matcher?

  expect { raise "oops" }.to raise_error
zealot128 commented 3 years ago

I have the same problem, because using the ActiveJob::TestHelpers uses that method underneath:

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'

what's the best method to replicate that functionality?

EDIT

Workaround for now:

RSpec.configure do |config|
   def assert_nothing_raised(&block)
      expect(&block).to_not raise_error
   end
end
JonRowe commented 3 years ago

It sounds like the module it is defined in has moved, there are a bunch of assertion modules we bring in to support various helpers.

doits commented 3 years ago

Method definition is located in ActiveSupport::Testing::Assertions.

So you can also work around it by:

RSpec.configure do |config|
  config.include ActiveSupport::Testing::Assertions
end
pirj commented 3 years ago

@benoittgt Do you think we can fix this for 6.1?

doits commented 3 years ago

This is fixed in Rails 6.1.1. https://github.com/rails/rails/pull/40780, nothing to do here 🎉

pirj commented 3 years ago

@doits, thanks for the heads-up. It is known to be resolved in Rails (see https://github.com/rspec/rspec-rails/pull/2398#issuecomment-748345156). Since Rails 6.1.1 is out, we can actually close this and if someone reports, we could recommend updating their Rails version. WDYT, @benoittgt ?

JonRowe commented 3 years ago

I think we should add the test from #2412 as a regression check, and if it passes with 6.1.1 close this.

pirj commented 3 years ago

Regression test added in #2455