-
Notifications
You must be signed in to change notification settings - Fork 0
/
minitest_cheat.rb
114 lines (95 loc) · 2.37 KB
/
minitest_cheat.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#
# Mark Bates: A Big Look at MiniTest http://www.confreaks.com/videos/2713-arrrrcamp2013-a-big-look-at-minitest
# minitest-rails howto starts 22 minutes in
#
require 'minitest/autorun'
# enable context blocks
class Minitest::Spec
class << self
alias :context :describe
end
end
class Foo
def add(x, y)
x + y
end
end
describe Foo do
let(:something) { [ 1, 2, 3 ] }
before do # before each test
@foo = 1
end
after do # after each test
@foo = nil
end
context '#add' do
it 'adds two numbers' do
Foo.new.add(1,2).must_equal 3
end
end
context 'skips' do
it 'this because no block'
it 'this explicitly' do
skip
end
it 'this explicitly' do
skip 'and outputs a reason'
end
end
context 'expectations' do
it 'uses must and wont' do
true.must_equal true
true.wont_equal false
end
it 'handles exceptions' do
# stabby lambda!
->{nil/2}.must_raise NoMethodError
end
end
# Minitest encourages you to build your own mocks with Struct/OpenStruct
context 'mocking/stubbing' do
let(:a_mock) { MiniTest::Mock.new }
let(:some_thing) { Struct.new(:name).new('Widget') } # struct
# let(:some_thing) { OpenStruct.new(name: 'Widget') } # OpenStruct
it 'mocks' do
a_mock.expect(:name, 'Widget') # expects call to name, returns widget
a_mock.name.must_equal 'Widget'
a_mock.verify # check whether it happened
end
it 'stubs with blocks' do
some_thing.name.must_equal 'Widget'
some_thing.stub(:name, 'Thingy') do
# stub applies inside this block
some_thing.name.must_equal 'Thingy'
end
end
it 'try stubs in pure ruby!' do
def some_thing.name # applies only to this instance
'Whassit'
end
some_thing.name.must_equal 'Whassit'
end
end
context 'custom assertions' do
it 'can use them' do
a = "Bob"
b = "Jane"
assert_clones(a,a)
refute_clones(a,b)
a.must_be_clones a
a.wont_be_clones b
end
end
end
module MiniTest::Assertions
def assert_clones a, b
assert a == b
end
def refute_clones a, b
refute a == b
end
end
# infect specific types when assertion makes sense only for them
# NOTE: infection expects 2 args!
Object.infect_an_assertion :assert_clones, :must_be_clones
Object.infect_an_assertion :refute_clones, :wont_be_clones