1

I'm starting a new Rails 7 app and have about 150 tests on Minitest.

I've seen in some places people using in the terminal of command rails test and other places recommending bundle exec rake test

I have some basic tests and when I run both commands in my terminal, the same exact things appear and I have the same amount of tests with the same test results.

So I'm wondering if there is any difference between the two on Rails 7?

Mathieu
  • 4,587
  • 11
  • 57
  • 112
  • 1
    Using the Rails executable `bin/rails test` is prefered in modern versions of Rails. There is no warning for this task but some of the other tasks will warn if you run them from Rake instead of Rails. https://guides.rubyonrails.org/v7.0/command_line.html#bin-rails-test – max Jul 06 '22 at 09:40
  • 2
    Rails 3 had a `script/rails` executable. Rails 4 moved it to `bin/rails` and also introduced `bin/rake`. Rails 5 then turned `bin/rails` into a wrapper for rake, so you only need one tool instead of two. On the other hand, `bundle exec rake` works for any Rails version, but it's more to type. – Stefan Jul 06 '22 at 10:53

1 Answers1

1

The difference between those commands is from where rails is called.

Running commands with bundler allows you to execute them without having the packages installed in your system.

For example, if you don't have rails installed globally you couldn't run commands using rails directly

rails test will raise the following error:

Rails is not currently installed on this system. To get the latest version, simply type:

$ sudo gem install rails

You can then rerun your "rails" command.

But if you run bundle exec rails test instead you should run your test suite perfectly because you're calling rails through bundle.

In your case using bundle exec rake test will ensure that you're using the same version of your Gemfile.lock.

  • You can read this post where it was a similar question https://stackoverflow.com/questions/23846493/what-is-the-difference-between-rails-s-and-bundle-exec-rails-s – Geovany Gameros Jul 06 '22 at 18:13