OSDN Git Service

Regular updates
[twpd/master.git] / minitest.md
1 ---
2 title: Minitest
3 category: Ruby
4 ---
5
6 ### Usage
7
8     require 'minitest/autorun'
9
10     describe "X" do
11       before do .. end
12       after do .. end
13       subject { .. }
14       let(:list) { Array.new }
15
16       it "should work" do
17         assert true
18       end
19     end
20
21 ### Specs (.must/.wont)
22
23     expect(x)
24     .must_be :==, 0
25     .must_equal b
26     .must_be_close_to 2.99999
27     .must_be_same_as b
28
29     .must_include needle
30     .must_be_empty
31
32     .must_be_kind_of
33     .must_be_instance_of
34     .must_be_nil
35     .must_match /regex/
36     .must_be :<=, 42
37     .must_respond_to msg
38
39     .must_be_silent  ( proc { "no stdout or stderr" }.must_be_silent)
40     .must_output "hi"
41
42     proc { ... }.must_output out_or_nil [, err]
43     proc { ... }.must_raise exception
44     proc { ... }.must_throw sym
45
46 ### Test
47
48     class TestHipster < Minitest::Test
49       def setup
50         @subject = ["silly hats", "skinny jeans"]
51       end
52
53       def teardown
54         @hipster.destroy!
55       end
56
57       def test_for_helvetica_font
58         assert_equal "helvetica!", @hipster.preferred_font
59       end
60
61       def test_not_mainstream
62         refute @hipster.mainstream?
63       end
64     end
65
66 ### Assertions
67
68     assert
69     assert_block { ... }
70     assert_empty
71     assert_equal 2, @size
72     assert_in_delta @size, 1, 1
73     assert_in_epsilon
74     assert_includes @list, "item"
75     assert_instance_of Array, @list
76     assert_kind_of Enumerable, @list
77     assert_match @str, /regex/
78     assert_nil
79     assert_operator @n, :==, 0
80     assert_output
81     assert_raises
82     assert_respond_to
83     assert_same
84     assert_send
85     assert_silent
86     assert_throws
87
88 ### Minitest::Mock
89
90 A simple and clean mock system. There two essential methods at our disposal: expect and verify.
91
92     require 'minitest/autorun'
93
94     describe Twipster, "Make every tweet a hipster tweet." do
95       before do
96         @twitter  = Minitest::Mock.new
97         @twipster = Twipster.new(@twitter)
98       end
99
100       it "should append a #lolhipster hashtag and update Twitter with our status" do
101         tweet = "Skyrim? Too mainstream."
102         @twitter.expect :update, true, ["#{tweet} #lolhipster"]
103         @twipster.submit(tweet)
104         assert @twitter.verify # verifies tweet and hashtag was passed to `@twitter.update`
105       end
106     end
107
108 ### Reporters
109
110     gem 'minitest-reporters'
111
112     require 'minitest/reporters'
113     Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
114
115     [Default, Spec, Progress, RubyMate, RubyMine, JUnit]