Use Minitest::Spec in Rails.
There are plenty out there, and I'm sure they all work just fine. I have personally never used any of them because it was never a big deal to insert Minitest::Spec into any Rails project manually with just a few lines of code. Fewer gems/lines of code your project = better. But it's still nice to have the convenience of a gem to get Rails testing started quickly.
This gem gets you PURE minitest spec in a whopping 50 lines of code, and that's counting blank lines, comments, require
s, and a (hopefully temporary) workaround for system tests.
There's no fancy tricks going on here.
This is plain jane minitest.
What you get is PURE spec awesomeness.
And by "PURE" I mean just describe
s. No more defining classes on your own.
- Get running in Rails with Minitest::Spec with minimal lines of code both in installation and the gem itself.
- Help with migrating from "Rails style" to Spec style. I don't recommend anyone convert any test suite to spec style for the sake of converting. You really don't gain all that much by converting.
- Support multiple styles (i.e. unit and spec). You're probably here to use Spec style, so use Spec style. Why would you want to mix and match? Don't define test classes. Use
describe
. - Support old Rails versions. Since I don't recommend converting, it doesn't seem worth it to support older Rails versions. I'm not going to explicitly define a minimum Rails version requirement, but for development I'm going to start with 5.1.1 (see the example app).
- (for now at least) Rails generators. I personally don't find them all that useful. If there is enough demand for it and/or someone contributes it, I might do it, but in the interest of keeping this gem small, I'm going to skip them.
Add this line to your application's Gemfile:
gem 'minispec-rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install minispec-rails
Once this gem is required, you can immediately start using it to describe
your tests.
There is 1 exception: system tests (see below).
The key to getting the right type of test is to describe the right thing.
Test type | Test subclass | describe example |
---|---|---|
Controller | ActionDispatch::IntegrationTest | describe MyController |
Job | ActiveJob::TestCase | describe MyJob |
Mailer | ActionMailer::TestCase | describe MyMailer |
System | ActionDispatch::SystemTestCase | describe 'Admin system' |
Integration | ActionDispatch::IntegrationTest | describe 'Customer integration' |
Anything else | ActiveSupport::TestCase | describe 'any object' |
See the example rails app.
Require this before defining ApplicationSystemTestCase
(see lib/minispec/rails/system_test.rb):
# test/application_system_test_case.rb.
require "test_helper"
require 'minispec/rails/system_test' # <- Add this line here.
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
end
- Ask Rails to add
run_load_hooks
for ActionDispatch::SystemTestCase so we can lazy load like the others. - Custom file types? I like file types with extensions like
spec/models/my_model.spec.rb
.
The gem is available as open source under the terms of the MIT License.