MiniTest: Spec Style, Mocking & MiniTest vs. RSpec
MiniTest::Spec Style
describe User do
before { @user = User.new(name: "Ada") }
it "has the given name" do
@user.name.must_equal "Ada"
end
end
# describe/it blocks compile down to the same Minitest::Test
# engine underneath -- both styles can run in the same suiteMocks & Stubs
mock = Minitest::Mock.new
mock.expect(:call, true, ["expected_arg"])
my_service.notify(mock)
mock.verify
# Stub: temporarily override a method's return value for a block
weather_api.stub(:current_temp, 72) do
assert_equal "It's 72°", forecast.summary
endWhy MiniTest Over RSpec, Sometimes
MiniTest ships with Ruby itself and favors plain method calls over RSpec's more elaborate DSL and metaprogramming -- appealing to teams wanting a lighter-weight, more "vanilla Ruby" testing approach with lower per-test overhead on large suites.
Keeping Tests Focused
A test method bundling many unrelated assertions can obscure exactly what broke -- a failure partway through can prevent later assertions from even running. Smaller, single-purpose tests keep failures diagnostic and easy to pinpoint.
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free