MinitestToRspec
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)
Example
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.
Install
gem install minitest_to_rspec
Usage
CLI
mt2rspec [--rails] [--mocha] source_file [target_file]
mt2rspec --help
Ruby
require 'minitest_to_rspec'
converter = MinitestToRspec::Converter.new(rails: false, mocha: false)
converter.convert("assert('banana')")
#=> "expect(\"banana\").to(be_truthy)"
Output
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.
Supported Assertions
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 |
Supported Mocha
| 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.
Acknowledgements
This project would not be possible without ruby_parser, sexp_processor, and ruby2ruby by Ryan Davis.