Converts minitest files to rspec.
- Selected assertions from Test::Unit, minitest, and ActiveSupport are converted to rspec-expectations.
- Selected methods from mocha are converted to rspec-mocks. (Experimental)
- Selected methods from shoulda-context
Input:
require 'test_helper'
class ArrayTest < ActiveSupport::TestCase
test "changes length" do
ary = []
assert_difference "ary.length" do
ary.push(:x)
end
end
end
Output:
require("spec_helper")
RSpec.describe(Array) do
it("changes length") do
ary = []
expect { ary.push(:x) }.to(change { ary.length })
end
end
You might not like the code style of the output. More on that below.
gem install minitest_to_rspec
mt2rspec [--rails] [--mocha] source_file [target_file]
mt2rspec --help
require 'minitest_to_rspec'
converter = MinitestToRspec::Converter.new(rails: false, mocha: false)
converter.convert("assert('banana')")
#=> "expect(\"banana\").to(be_truthy)"
The only goal is correctness. Code style is not a consideration. Providing the level of configuration necessary to make everyone happy would be a huge distraction from the main purpose.
After conversion, I recommend using rubocop's awesome --auto-correct
feature to apply your preferred code style.
Comments are discarded by ruby_parser, so we have no way of preserving them.
Selected assertions from minitest, Test::Unit, and ActiveSupport. See doc/supported_assertions.md for rationale. Contributions are welcome.
Assertion | Arity | Source |
---|---|---|
assert | ||
assert_difference | 1,2 | |
assert_equal | 2,3 | Test::Unit |
assert_not_equal | 2,3 | Test::Unit |
assert_match | ||
assert_nil | ||
assert_not_nil | ||
assert_no_difference | ActiveSupport | |
assert_nothing_raised | Test::Unit | |
assert_raise | 0..2 | Test::Unit |
assert_raises | 0..2 | Minitest |
refute | ||
refute_equal | ||
refute_raise | ||
refute_raises |
Mocha | Arity | Block | Notes |
---|---|---|---|
any_instance | 0 | n/a | |
expects | 1 | n/a | |
once | 0 | n/a | |
stub | 0,1,2 | no | |
stub_everything | 0,1,2 | no | Uses as_null_object , not the same. |
stubs | 1 | n/a | |
twice | 0 | n/a |
To do: at_least, never, raises, etc.
Mocha | Arity | Block | Notes |
---|---|---|---|
context | 1 | yes | |
setup | 1,2 | no | |
should | 1,2 | yes |
This project would not be possible without ruby_parser, sexp_processor, and ruby2ruby by Ryan Davis.