Code Monkey home page Code Monkey logo

Comments (10)

Earlopain avatar Earlopain commented on June 14, 2024 1

Rails itself doesn't document assert_raise in their testing guides https://guides.rubyonrails.org/testing.html
The alias itself was added 12 years ago and marked as for backwards compatibility reasons rails/rails@aa7857b, apparently from before rails was using minitest.

from rails-style-guide.

koic avatar koic commented on June 14, 2024

I'm not sure whether assert_raises or assert_raise is preferable as the default. Please discuss it in the style guide first.

from rails-style-guide.

andyw8 avatar andyw8 commented on June 14, 2024

assert_raise is an alias for Rails' own assert_raises, which adds support for a match parameter, so it's not Minitest's assert_raises:

https://github.com/rails/rails/blob/23938052acd773fa24068debe56cd892cbf8d868/activesupport/lib/active_support/testing/assertions.rb#L34-L39

But I would support a guideline to prefer assert_raises over assert_raise for consistency.

from rails-style-guide.

koic avatar koic commented on June 14, 2024

rubocop/rubocop-rails#913 might be added for background context.

from rails-style-guide.

koic avatar koic commented on June 14, 2024

For example, in the rails/rails repository, both assert_raise and assert_raises are used. If the Rails development team has a consistent preference for assert_raises, I feel it should be respected. e.g.

Use assert_not methods instead of refute.

https://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions

But, I'm not sure what the actual situation is.

from rails-style-guide.

Earlopain avatar Earlopain commented on June 14, 2024

Well, rails doesn't accept cosmetic changes. If someone where to come along wanting to change it the pr would just be rejected.

Is there something which collects rails applications like https://github.com/eliotsykes/real-world-rails? I have seen this repo come up in the past but it hasn't been maintained for the last few years.

from rails-style-guide.

koic avatar koic commented on June 14, 2024

The Ruby API itself is primarily named using the base form of verbs. For instance, in Ruby 3.2, File.exists was removed, leaving only File.exist. Given this, some might prefer assert_raise over assert_raises.

from rails-style-guide.

Earlopain avatar Earlopain commented on June 14, 2024

That does indeed seem to be the prefered way for the ruby project itself. ruby/ruby@068f312 ruby/ruby@f159bbd

A code search on github for assert_raise and assert_raises also reveals that assert_raise has more usages than I would have expected:
assert_raises: 14k + 10.4k
assert_raise: 7.9k + 5.2k

Per these numbers assert_raises is used about twice as often as assert_raise. But then again these searches also include projects that don't use Rails. I don't think you're able to query for rails specifically, or get the numbers of actual matches instead of the amount of times the query appears in a file.

Personally I would still go with assert_raises since that is the method defined by minitest. But it looks like if this cop would be implemented there needs to be a config option so you're able to go with the variant you prefer.

from rails-style-guide.

koic avatar koic commented on June 14, 2024

When implementing a cop, default is important for users. Respecting Ruby's API naming and choosing assert_raise seems better, but the best choice remains uncertain.

from rails-style-guide.

Earlopain avatar Earlopain commented on June 14, 2024

When implementing cops, is there precedence for disabled by default and a required config key? Early adopters could opt in and must set their prefered style explicitly.

It's not obvious which variant is better but I'd still like to try and work on this since it'll eventually go one way or another. If/When a conclusion is reached the config can be changed.

from rails-style-guide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.