cheatsheets/minitest.md

116 lines
2.4 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
2013-01-24 21:04:38 +00:00
title: Minitest
2015-11-24 05:02:17 +00:00
category: Ruby
2013-01-24 21:04:38 +00:00
---
### Usage
require 'minitest/autorun'
describe "X" do
2013-08-24 07:43:10 +00:00
before do .. end
after do .. end
subject { .. }
let(:list) { Array.new }
2013-01-24 21:04:38 +00:00
it "should work" do
assert true
end
end
2014-02-25 10:32:14 +00:00
### Specs (.must/.wont)
2013-01-24 21:04:38 +00:00
2016-03-28 06:56:43 +00:00
expect(x)
2014-02-25 10:32:14 +00:00
.must_be :==, 0
2013-08-24 07:43:10 +00:00
.must_equal b
.must_be_close_to 2.99999
.must_be_same_as b
2013-01-24 21:04:38 +00:00
2013-08-24 07:43:10 +00:00
.must_include needle
.must_be_empty
2013-01-24 21:04:38 +00:00
.must_be_kind_of
2013-08-24 07:43:10 +00:00
.must_be_instance_of
.must_be_nil
.must_match /regex/
.must_be :<=, 42
.must_respond_to msg
.must_be_silent ( proc { "no stdout or stderr" }.must_be_silent)
.must_output "hi"
2013-01-24 21:04:38 +00:00
proc { ... }.must_output out_or_nil [, err]
proc { ... }.must_raise exception
proc { ... }.must_throw sym
### Test
2013-01-24 21:04:38 +00:00
class TestHipster < Minitest::Test
2014-03-26 12:44:55 +00:00
def setup
@subject = ["silly hats", "skinny jeans"]
end
2014-02-25 10:32:14 +00:00
2014-03-26 12:44:55 +00:00
def teardown
@hipster.destroy!
end
2014-02-25 10:32:14 +00:00
2014-03-26 12:44:55 +00:00
def test_for_helvetica_font
assert_equal "helvetica!", @hipster.preferred_font
end
2014-02-25 10:32:14 +00:00
2014-03-26 12:44:55 +00:00
def test_not_mainstream
refute @hipster.mainstream?
end
2014-02-25 10:32:14 +00:00
end
### Assertions
assert
assert_block { ... }
assert_empty
assert_equal 2, @size
assert_in_delta @size, 1, 1
assert_in_epsilon
assert_includes @list, "item"
assert_instance_of Array, @list
assert_kind_of Enumerable, @list
assert_match @str, /regex/
assert_nil
assert_operator @n, :==, 0
assert_output
assert_raises
assert_respond_to
assert_same
assert_send
assert_silent
assert_throws
### Minitest::Mock
2014-02-25 10:32:14 +00:00
A simple and clean mock system. There two essential methods at our disposal: expect and verify.
require 'minitest/autorun'
describe Twipster, "Make every tweet a hipster tweet." do
before do
@twitter = Minitest::Mock.new
2014-02-25 10:32:14 +00:00
@twipster = Twipster.new(@twitter)
end
it "should append a #lolhipster hashtag and update Twitter with our status" do
tweet = "Skyrim? Too mainstream."
@twitter.expect :update, true, ["#{tweet} #lolhipster"]
@twipster.submit(tweet)
assert @twitter.verify # verifies tweet and hashtag was passed to `@twitter.update`
end
end
### Reporters
gem 'minitest-reporters'
require 'minitest/reporters'
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
2013-01-24 21:04:38 +00:00
2014-02-25 10:32:14 +00:00
[Default, Spec, Progress, RubyMate, RubyMine, JUnit]