Skip to content

ordinaryzelig/minispec-rails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minispec::Rails

Use Minitest::Spec in Rails.

Why another minitest rails gem?

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, requires, 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 describes. No more defining classes on your own.

Goals

  • Get running in Rails with Minitest::Spec with minimal lines of code both in installation and the gem itself.

NOT Goals

  • 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.

Installation

Add this line to your application's Gemfile:

gem 'minispec-rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install minispec-rails

Usage

Once this gem is required, you can immediately start using it to describe your tests. There is 1 exception: system tests (see below).

Spec Types

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'

Example app

See the example rails app.

System tests

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

TODO

  • 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.

License

The gem is available as open source under the terms of the MIT License.

About

Minimal, PURE MiniTest::Spec for Rails tests

Resources

License

Stars

Watchers

Forks

Packages

No packages published