Cheat Sheet: Minitest
WIP — check back soon!
Writing a unit-style test:
class TestArrays < Minitest::Test
def test_array_includes_symbols
assert_includes [:a, :b], :a
end
end
Writing a spec-style test:
describe "namespace support" do
def to_js(string)
_(Ruby2JS.convert(string, eslevel: 2017, filters: []).to_s)
end
describe "open modules" do
it "should extend modules" do
to_js( 'module M; def f(); end; end;' +
'module M; def g(); end; end').
must_equal('const M = {f() {}}; ' +
'M.g = function() {}');
end
end
end
The nice thing is, you even can leverage the expressive DSL of spec tests and expectations even in a unit test class.
###
It’s common to subclass tests from a common superclass you can augment with your own helpers and other setup code:
require "minitest_helper"
class TestAllTheThings < MyUnitTest
# test cases here
end
# minitest_helper.rb
class MyUnitTest < Minitest::Test
end
Checking Your Data
Assertion / Expectation | Description | Example |
---|---|---|
|
Validates if the collection is empty Fails if the collection is empty |
|
|
Validates if two values match according to the equality ( Fails if two values match according to the equality ( |
|
|
Validates if the value can found within the collection Fails if the value can be found within the collection |
|
|
Validates if the test object is exactly an instance of the expected class Fails if the test object is exactly an instance of the expected class |
|
|
Validates if the test object is an instance of the expected class (or subclass) Fails if the test object is an instance of the expected class (or subclass) |
|
|
Validates if the value matches a regular expression (according to the Fails if the value matches a regular expression (according to the |
|
|
Validates if the value is Fails if the value is |
|
|
Validates if the code executed within the block raises the provided exception |
|