Ruby minitest
From wikinotes
A testing framework.
See Also:
Documentation
api docs http://docs.seattlerb.org/minitest/ assertion docs http://docs.seattlerb.org/minitest/Minitest/Assertions.html github https://github.com/seattlerb/minitest
Tutorials
https://brandonhilkert.com/blog/7-reasons-why-im-sticking-with-minitest-and-fixtures-in-rails/
Sample
require "minitest/autorun" class TestMeme < Minitest::Test def setup @meme = Meme.new end def test_that_kitty_can_eat assert_equal "OHAI!", @meme.i_can_has_cheezburger? end end
Usage
# run single testfile ruby -I.:test test/minitest/test_minitest_test.rb # run select testfiles bundle exec ruby -I.:test -e "ARGV.each{|f| require f}" file1 file1class MyTest < Minitest::Test def test_does_thing assert_equal(1, 2) end end MyTest.new("test_does_thing").run # run test with setup/teardown hooks
Basics
- in minitest, refute is the opposite of assert.
nearly all assertions have equivalent refute methods.
setup/teardown
See docs http://docs.seattlerb.org/minitest/Minitest/Test/LifecycleHooks.html
class UserTest < Minitest::Test # before each test def setup @name = "alex" @age = "30" end # after each test def teardown @counter = 0 end end
Skip
def test_something skip "skipping test because..." end
Assertions
See http://docs.seattlerb.org/minitest/Minitest/Assertions.html
NOTE:
item on left is expected result.
Common Assertions
assert result # result is truthy assert_equal "foo", result # result equals value assert_match /[a-zA-Z]/, result # result matches regex assert_raises(StandardError, "reason") { # .. code that raises exception ... }Unfamiliar Assertions
Mocks
Dummy objects that expect that method name is called.
Returns canned response, optionally validates called-with-args.See http://docs.seattlerb.org/minitest/Minitest/Mock.html
meme = Minitest::Mock.new # build mock meme.exepect :method_name, "return value" # expect 'method_name' to be called, set returnval of method meme.verify # assert `meme.method_name` was called
Stubs
Dummy objects whose methods give canned responses.
Time.stub :now, Time.at(0) do # stub goes away once the block is done assert obj_under_test.stale? end