OSDN Git Service

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