Chris Mc
Chris Mc

Reputation: 85

Minitest-Rails Spec-Style Tests by Default

Question: Using Rails 5 & Minitest-Rails is there a way to default new Rails apps to default to Spec-style testing?

I teach TDD and it's annoying to have to have the students convert each time we make a new app.

Upvotes: 2

Views: 645

Answers (4)

Schadenfred
Schadenfred

Reputation: 136

Looks like you've already done the hard work of answering your question. Though if you're teaching a class with an opinionated group of test gems, and a modified test_helper.rb and a modified application.rb, you may wish to consider writing your own gem that your students can add to their Gemfile. The gem could have the test gems you want as dependencies, and then they can install everything else they need with something like:

bin/rails generate <gem_name>:install

Here's a gem I wrote that you can fork or modify or just use as inspiration.

https://github.com/schadenfred/testable

I actually stole your config code for the above gem, which you can see expressed in inside a generator that lives here:

lib/generators/installer/install_generator.rb

Upvotes: 1

Chris Mc
Chris Mc

Reputation: 85

Ok so @sonna had 90% of what I was looking for.

I ended up with help creating a .railsrc file with

-d postgresql
-m ~/.template.rb

And a template with:

# .template.rb
# Gems we've talked about in class, but which have absolutely nothing to do
# with setting up spec-style testing.
# Included here for convenience.
gem_group :development do
  # Improve the error message you get in the browser
  gem 'better_errors'

  # Use pry for rails console
  gem 'pry-rails'
end

# Add some extra minitest support
gem_group :test do
  gem 'minitest-rails'
  gem 'minitest-reporters'
end

# Add some code to some files to support spec-style testing
# For these injections, indentation matters!
inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do
  <<-'RUBY'
    # Force new test files to be generated in the minitest-spec style
    config.generators do |g|
      g.test_framework :minitest, spec: true
    end
  RUBY
end

# Things to do after all the gems have been installed
after_bundle do
  # Run rails generate minitest:install
  generate "minitest:install", "--force"

  # Add minitest reporters support. This must be run after
  # rails generate minitest:install, because that command
  # changes test/test_helper.rb
  inject_into_file 'test/test_helper.rb', after: 'require "minitest/rails"' do
    <<-'RUBY'

require "minitest/reporters"  # for Colorized output

#  For colorful output!
Minitest::Reporters.use!(
  Minitest::Reporters::SpecReporter.new,
  ENV,
  Minitest.backtrace_filter
)
    RUBY
  end
end

This sets up my project with postgres for DB, and Minitest-rails using spec-style tests and includes minitest-reporters.

Upvotes: 0

Chris Mc
Chris Mc

Reputation: 85

It looks like in config/application.rb you just have to add:

config.generators do |g| g.test_framework :minitest, spec: true end

However there's not an automatic way to make Minitest-Rails default to spec style testing.

I could go to rspec, but would rather stay with Minitest for the moment as we teach our students Minitest from the beginning.

Upvotes: 1

sonna
sonna

Reputation: 620

You could create a template.rb file with following configuration:

gem_group :development, :test do
  gem "rspec-rails"
end

after_bundle do
  `rails g rspec:install`
end

And then build a new Rails project using the following command

rails new my_app -T -m /path/to/template.rb

It will build a new Rails application, add Rails RSpec gem to its Gemfile and execute the install step for RSpec.

Otherwise you could provide a pre-built Rails Git repository and build on top of that.

References:

Upvotes: 2

Related Questions