Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cop idea: discourage the use of assert_/refute_ Minitest matchers #7

Open
pirj opened this issue Nov 15, 2022 · 12 comments
Open

Cop idea: discourage the use of assert_/refute_ Minitest matchers #7

pirj opened this issue Nov 15, 2022 · 12 comments
Labels

Comments

@pirj
Copy link
Member

pirj commented Nov 15, 2022

Suggested by @Darhazer

RSpec Expectations allows using assert_/refute_ Minitest matchers
However, RSpec comes with its matchers that pretty much cover the same:

- assert_equal a, b
+ expect(a).to eq(b)

A notable difference is that Minitest allows for an optional failure message.
It's a lesser known RSpec feature, but it allows it, too:

- assert_equal a, b, "must be equal"
+ expect(a).to(eq(b), "must be equal")
ydah referenced this issue in ydah/rubocop-rspec Nov 17, 2022
@bquorning
Copy link
Contributor

Do we know why rspec-expectations provide this feature? Is it going to be deprecated?

One of the non-goals of rubocop-rspec is

Enforcing should vs. expect syntax

and I wonder if the suggested cop doesn’t fall under this category.

@pirj
Copy link
Member Author

pirj commented Nov 17, 2022

Fair enough.
RSpeс has a сonfiguration option for that:

RSpec.configure do |config|
  config.expect_with :minitest
end

which is exclusively using rspec-expectations in the default project spec/spec_helper.rb initializer (and most of the projects, I assume):

config.expect_with :rspec do |expectations|

If with this it's still possible to use Minitest's assertions, there's probably something wrong.

However, rspec-rails is using Rails' Minitests assertions internally, and I can't tell off the top of my head if those can be encapsulated and unintentionally leak, or there's just no simple way to encapsulate them and use rspec-expectations' matchers exclusively.

ydah referenced this issue in ydah/rubocop-rspec Nov 18, 2022
@pirj
Copy link
Member Author

pirj commented Nov 18, 2022

Indeed, it's coming from rspec-rails:

module RSpec
  module Rails
    # @api public
    # Common rails example functionality.
    module RailsExampleGroup
      # ...
      include RSpec::Rails::MinitestAssertionAdapter

I'll give it a shot to only include it if expect_with is configured to also allow :minitest.

@pirj
Copy link
Member Author

pirj commented Nov 19, 2022

rspec/rspec-rails#2639

@pirj
Copy link
Member Author

pirj commented Nov 19, 2022

Fails, e.g.:

1) UploadsController DELETE #destroy redirects to the uploads list
     Failure/Error: expect(response).to redirect_to(uploads_url)

     NoMethodError:
       undefined method `assert' for #<RSpec::ExampleGroups::UploadsController::DELETEDestroy "redirects to the uploads list" (./spec/controllers/uploads_controller_spec.rb:138)>
     # ./spec/controllers/uploads_controller_spec.rb:141:in `block (3 levels) in <top (required)>'

If it's impossible to restrict this on RSpec level, I suppose we're left with static analysis.
Unless I misunderstood your argument, @bquorning

ydah referenced this issue in ydah/rubocop-rspec Dec 4, 2022
ydah referenced this issue in ydah/rubocop-rspec Dec 12, 2022
@pirj
Copy link
Member Author

pirj commented Dec 13, 2022

There are also a few aliases that Rails defines here. I guess the cop could make this list configurable, and rubocop-rspec-rails would amend the list supplying those Rails ones? But how would auto-correction work? Or is it better to somehow make the cop extensible from other extensions?

ydah referenced this issue in ydah/rubocop-rspec Dec 13, 2022
@bquorning
Copy link
Contributor

I am still not sure which problem this PR fixes. Minitest matchers are not loaded by default. This example:

❯ ruby -r rspec/autorun -e 'describe "foo" do; it "bar" do; assert_equal(1, 2); end; end'

gives me the error undefined method `assert_equal' for ...

@pirj
Copy link
Member Author

pirj commented Dec 13, 2022

They are if you use rspec-rails.

It makes sense to re-target this cop to be RSpec/Rails/MinitestAssertions and eventually moving it to rubocop-rspec-rails under RSpecRails/ namespace.

ydah referenced this issue in ydah/rubocop-rspec Dec 13, 2022
@pirj pirj reopened this Dec 13, 2022
@pirj
Copy link
Member Author

pirj commented Dec 13, 2022

Let's keep this open, since there are a few more matchers than can be covered.

@mvz
Copy link
Contributor

mvz commented Apr 14, 2023

Please note that the order of the arguments of assert_equals is reversed from the expect syntax: The actual is the second argument. See http://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-assert_equal

@pirj
Copy link
Member Author

pirj commented Jan 11, 2024

test-unit assertions, for completeness https://www.rubydoc.info/gems/test-unit/2.3.2/Test/Unit/Assertions

@G-Rath
Copy link
Contributor

G-Rath commented Jan 12, 2024

Also here's the Rails aliased assertion methods: https://github.com/rails/rails/blob/main/activesupport/lib/active_support/test_case.rb#L153

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants