rubocop / minitest-style-guide

Best practices for writing your tests
https://minitest.rubystyle.guide
70 stars 15 forks source link

Use `assert_same` and `refute_same` instead of `equal?` #53

Closed tejasbubane closed 1 year ago

tejasbubane commented 1 year ago

https://github.com/rubocop/rubocop-minitest/issues/215

andyw8 commented 1 year ago

Repeating my reply from https://github.com/rubocop/rubocop-minitest/issues/215 for visibility:

I have a slight concern here: same is less explicit than equal?. It's not obvious which kind of comparison is being made (==, eql?, etc. ) unless you look at the implementation or docs.

tejasbubane commented 1 year ago

I have a slight concern here: same is less explicit than equal?. It's not obvious which kind of comparison is being made (==, eql?, etc. ) unless you look at the implementation or docs.

This applies to assert_equal too. To begin with ==, eql?, equal?, etc are equally confusing for newcomers. I feel same means same object which is checked by equal?.

andyw8 commented 1 year ago

That's true. But assert_equal is much more common, so it's better understood.

I'm not against this as a new cop, but there's some nuance, and I'm not sure how best to handle it.

koic commented 1 year ago

I understand @andyw8's concern. However, equal? is for confirming object identity, and "asssert_same" seems to be a reasonable naming. After all, it is a point to keep in mind the difference between object identity comparison and object value comparison. I remember having a similar talk when writing the following rule: https://rubystyle.guide/#identity-comparison