-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Migrate from RSpec to Minitest
Why
Basically the gem is simple and all we need here is few asserts, verifying correct state and output.
Current State
- ~60 RSpec tests across 3 spec files
- Dependencies:
rspec,rubocop-rspec - RSpec features used:
stub_const,allow/receivemocking,let,contextblocks,aroundhooks
Pros for Migration
1. Only asserts
RSpec's DSL is expressive but actually is not needed for the gem. Tests should be simple and fast. RSpec DSL is not needed for that case. Lets use following example for simplicity illustration:
# RSpec
expect(result.valid?).to be true
expect(result.short_name).to eq 'TC'
# Minitest
assert result.valid?
assert_equal 'TC', result.short_name2. Faster boot time
For a small gem you run tests constantly. RSpec's boot overhead (loading DSL, matchers, etc.) adds up. Minitest loads in milliseconds.
3. Fewer dependencies
Remove from Gemfile:
rspec(~3 gems)rubocop-rspec
Add:
minitest(usually bundled with Ruby)mochaorminitest-mock(for stubbing, if needed)
4. Rails-native patterns
If anyone uses this gem with Rails, they're already familiar with Minitest assertions. One less context switch.
5. No magic
RSpec's described_class, implicit subjects, shared contexts, and matcher DSL are powerful but obscure what's actually happening. Minitest forces explicit, readable tests.
Migration Considerations
RSpec features that need replacement:
| RSpec | Minitest Equivalent |
|---|---|
describe/context |
class FooTest < Minitest::Test |
it "does thing" |
def test_does_thing |
expect(x).to eq(y) |
assert_equal y, x |
expect(x).to be true |
assert x |
expect(x).to be_nil |
assert_nil x |
let(:foo) { ... } |
`def foo; @foo |
before |
def setup |
after |
def teardown |
stub_const |
Define test modules inline or use Object.const_set |
allow(x).to receive(:y) |
mocha gem or Minitest::Mock |
around |
setup/teardown with manual cleanup |
Files to migrate:
-
spec/spec_helper.rb→test/test_helper.rb -
spec/lazy_names/line_validator_spec.rb→test/lazy_names/line_validator_test.rb -
spec/lazy_names/ruby_loader_spec.rb→test/lazy_names/ruby_loader_test.rb -
spec/integration/ruby_config_spec.rb→test/integration/ruby_config_test.rb
Config files to update/remove:
- Remove
.rspec - Remove
.rspec_status - Update
Gemfile - Update
Rakefile(task definition) - Update
.rubocop.yml(remove rspec rules)
References
- Minitest docs
- Mocha for mocking (if
stub_const-style behavior needed)