test_unit-given 0.1.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +44 -0
- data/lib/test/unit/given/simple.rb +70 -0
- data/lib/test/unit/given/version.rb +1 -1
- data/test/test_simple_given.rb +17 -0
- metadata +8 -8
data/README.rdoc
CHANGED
@@ -111,6 +111,50 @@ Finally, you can re-use blocks, and use And to create richer expressions:
|
|
111
111
|
}
|
112
112
|
end
|
113
113
|
|
114
|
+
=== What about mocks?
|
115
|
+
|
116
|
+
Mocks create an interesting issue, because the "assertions" are the mock expectations you setup before you call the method under test. This means that the "then" side of things is out of order.
|
117
|
+
|
118
|
+
class CircleTest < Test::Unit::Given::TestCase
|
119
|
+
test_that "our external diameter service is being used" do
|
120
|
+
Given {
|
121
|
+
@diameter_service = mock()
|
122
|
+
@diameter_service.expects(:get_diameter).with(10).returns(400)
|
123
|
+
@cirlce = Circle.new(10,@diameter_service)
|
124
|
+
}
|
125
|
+
When {
|
126
|
+
@diameter = @cirlce.diameter
|
127
|
+
}
|
128
|
+
Then {
|
129
|
+
// assume mocks were called
|
130
|
+
}
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
This is somewhat confusing. We could solve it like so:
|
135
|
+
|
136
|
+
class CircleTest < Test::Unit::Given::TestCase
|
137
|
+
test_that "our external diameter service is being used" do
|
138
|
+
Given {
|
139
|
+
@diameter_service = mock()
|
140
|
+
}
|
141
|
+
When mocks_are_called
|
142
|
+
Then {
|
143
|
+
@diameter_service.expects(:get_diameter).with(10).returns(400)
|
144
|
+
}
|
145
|
+
Given {
|
146
|
+
@cirlce = Circle.new(10,@diameter_service)
|
147
|
+
}
|
148
|
+
When {
|
149
|
+
@diameter = @cirlce.diameter
|
150
|
+
}
|
151
|
+
Then mocks_shouldve_been_called
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
Although both <tt>mocks_are_called</tt> and <tt>mocks_shouldve_been_called</tt> are no-ops,
|
156
|
+
they allow our tests to be readable and make clear what the assertions are that we are making.
|
157
|
+
|
114
158
|
=== What about block-based assertions, like +assert_raises+
|
115
159
|
|
116
160
|
class CircleTest < Test::Unit::Given::TestCase
|
@@ -94,6 +94,76 @@ module Test
|
|
94
94
|
Given(existing_block,&block)
|
95
95
|
end
|
96
96
|
|
97
|
+
# Public: Continue the previous Given/When/Then in a new block. The reason
|
98
|
+
# you might do this is if you wanted to use the existing block form of a
|
99
|
+
# Given/When/Then, but also need to do something custom, and that custom thing
|
100
|
+
# contradicts or overrides the flow, so an "And" wouldn't read properly. This allows you to
|
101
|
+
# write your test more fluently.
|
102
|
+
#
|
103
|
+
# existing_block - a callable object (e.g. a Proc) that will be called immediately
|
104
|
+
# by this Then. If nil, &block is expected to be passed
|
105
|
+
# block - a block given to this call that will be executed immediately
|
106
|
+
# by this Then. If existing_block is non-nil, this is ignored
|
107
|
+
#
|
108
|
+
# Examples
|
109
|
+
#
|
110
|
+
# given_block = Given {
|
111
|
+
# @options = {
|
112
|
+
# :foo => 'bar',
|
113
|
+
# :quux => 'baz',
|
114
|
+
# }
|
115
|
+
# }
|
116
|
+
# Given then_block
|
117
|
+
# And {
|
118
|
+
# @name = 'Foo'
|
119
|
+
# }
|
120
|
+
# But {
|
121
|
+
# @options[:quux] = 'BAZ'
|
122
|
+
# }
|
123
|
+
#
|
124
|
+
# Returns the block that was executed
|
125
|
+
def But(existing_block=nil,&block)
|
126
|
+
Given(existing_block,&block)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Public: Used to make clear the structure of tests using mocks.
|
130
|
+
# This returns a no-op, and is intended to be used with a "When"
|
131
|
+
# to delineate the creation of a mock in a Given, and the
|
132
|
+
# expectations of a mock in a "Then"
|
133
|
+
#
|
134
|
+
# Example:
|
135
|
+
#
|
136
|
+
# Given {
|
137
|
+
# @google = mock()
|
138
|
+
# }
|
139
|
+
# When mocks_are_called
|
140
|
+
# Then {
|
141
|
+
# @google.expects(:search).with('foo').returns('bar')
|
142
|
+
# }
|
143
|
+
# Given {
|
144
|
+
# @my_search = Search.new(@google)
|
145
|
+
# }
|
146
|
+
# When {
|
147
|
+
# @result = @my_search.find('foo')
|
148
|
+
# }
|
149
|
+
# Then {
|
150
|
+
# assert_equal 'Found bar',@result
|
151
|
+
# }
|
152
|
+
# And mocks_shouldve_been_called
|
153
|
+
def mocks_are_called
|
154
|
+
lambda {}
|
155
|
+
end
|
156
|
+
|
157
|
+
# Similar to #mocks_are_called, this is used to make clear what
|
158
|
+
# you are testing and what the assertions are. Since many Ruby mock
|
159
|
+
# frameworks do not require an explicit "verify" step, you often have tests
|
160
|
+
# that have no explicit asserts, the assertions being simply that the mocks were called
|
161
|
+
# as expected. This step, which is a no-op, allows you to document that you are
|
162
|
+
# expecting mocks to be called. See the example in #mocks_are_called for usage.
|
163
|
+
def mocks_shouldve_been_called
|
164
|
+
lambda {}
|
165
|
+
end
|
166
|
+
|
97
167
|
private
|
98
168
|
|
99
169
|
def call_block_param_or_block_given(existing_block,&block)
|
data/test/test_simple_given.rb
CHANGED
@@ -9,6 +9,7 @@ class TestSimpleGiven < Test::Unit::Given::TestCase
|
|
9
9
|
}
|
10
10
|
And {
|
11
11
|
@y = nil
|
12
|
+
@z = 10
|
12
13
|
}
|
13
14
|
When {
|
14
15
|
@x = 4
|
@@ -16,12 +17,28 @@ class TestSimpleGiven < Test::Unit::Given::TestCase
|
|
16
17
|
And {
|
17
18
|
@y = 10
|
18
19
|
}
|
20
|
+
But {
|
21
|
+
@z # not assigned
|
22
|
+
}
|
19
23
|
Then {
|
20
24
|
assert_equal 4,@x
|
21
25
|
}
|
22
26
|
And {
|
23
27
|
assert_equal 10,@y
|
24
28
|
}
|
29
|
+
But {
|
30
|
+
assert_equal 10,@z
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_mock_support
|
35
|
+
Given { @x = 4 }
|
36
|
+
When mocks_are_called
|
37
|
+
Then { }
|
38
|
+
Given { @y = 4 }
|
39
|
+
When { @y = 10 }
|
40
|
+
Then { assert_equal 10,@y }
|
41
|
+
And mocks_shouldve_been_called
|
25
42
|
end
|
26
43
|
|
27
44
|
def test_cannot_use_locals
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_unit-given
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-19 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rdoc
|
16
|
-
requirement: &
|
16
|
+
requirement: &70252391815780 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70252391815780
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70252391815360 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70252391815360
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: simplecov
|
38
|
-
requirement: &
|
38
|
+
requirement: &70252391814940 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70252391814940
|
47
47
|
description: We don't need no stinkin' RSpec! Get all the fluency you want in your
|
48
48
|
Test::Unit tests, with no magic required, using straight Ruby syntax
|
49
49
|
email:
|