flexmock 1.3.3 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.autotest +3 -0
- data/.gitignore +14 -0
- data/.togglerc +7 -0
- data/.travis.yml +5 -0
- data/.yardopts +2 -0
- data/CHANGES +11 -0
- data/Gemfile +1 -4
- data/README.md +39 -11
- data/Rakefile +6 -217
- data/doc/examples/rspec_examples_spec.rb +244 -0
- data/doc/examples/test_unit_examples_test.rb +240 -0
- data/doc/jamis.rb +591 -0
- data/flexmock.gemspec +33 -0
- data/lib/flexmock.rb +0 -1
- data/lib/flexmock/composite_expectation.rb +1 -1
- data/lib/flexmock/core.rb +3 -7
- data/lib/flexmock/core_class_methods.rb +5 -1
- data/lib/flexmock/default_framework_adapter.rb +2 -2
- data/lib/flexmock/expectation.rb +29 -3
- data/lib/flexmock/expectation_director.rb +1 -1
- data/lib/flexmock/minitest.rb +13 -0
- data/lib/flexmock/minitest_extensions.rb +26 -0
- data/lib/flexmock/minitest_integration.rb +111 -0
- data/lib/flexmock/mock_container.rb +1 -2
- data/lib/flexmock/partial_mock.rb +61 -104
- data/lib/flexmock/recorder.rb +1 -2
- data/lib/flexmock/rspec.rb +6 -3
- data/lib/flexmock/test_unit_integration.rb +14 -0
- data/lib/flexmock/validators.rb +5 -4
- data/lib/flexmock/version.rb +1 -9
- data/rakelib/metrics.rake +40 -0
- data/rakelib/preview.rake +4 -0
- data/rakelib/tags.rake +18 -0
- data/todo.txt +20 -0
- metadata +61 -86
- data/Gemfile.lock +0 -20
- data/doc/examples/rspec_examples_spec.rdoc +0 -245
- data/doc/examples/test_unit_examples_test.rdoc +0 -241
- data/test/aliasing_test.rb +0 -66
- data/test/assert_spy_called_test.rb +0 -119
- data/test/base_class_test.rb +0 -71
- data/test/based_partials_test.rb +0 -51
- data/test/container_methods_test.rb +0 -118
- data/test/default_framework_adapter_test.rb +0 -38
- data/test/demeter_mocking_test.rb +0 -191
- data/test/deprecated_methods_test.rb +0 -225
- data/test/examples_from_readme_test.rb +0 -157
- data/test/expectation_description_test.rb +0 -80
- data/test/extended_should_receive_test.rb +0 -69
- data/test/flexmodel_test.rb +0 -54
- data/test/mock_builder_test.rb +0 -68
- data/test/naming_test.rb +0 -84
- data/test/new_instances_test.rb +0 -215
- data/test/object_extensions_test.rb +0 -25
- data/test/partial_mock_test.rb +0 -458
- data/test/record_mode_test.rb +0 -158
- data/test/redirect_error.rb +0 -16
- data/test/rspec_integration/integration_spec.rb +0 -56
- data/test/rspec_integration/spy_example_spec.rb +0 -207
- data/test/samples_test.rb +0 -283
- data/test/should_ignore_missing_test.rb +0 -84
- data/test/should_receive_test.rb +0 -1155
- data/test/spys_test.rb +0 -215
- data/test/symbol_extensions_test.rb +0 -8
- data/test/test_class_extensions.rb +0 -34
- data/test/test_setup.rb +0 -92
- data/test/test_unit_integration/auto_test_unit_test.rb +0 -42
- data/test/test_unit_integration/minitest_teardown_test.rb +0 -14
- data/test/tu_integration_test.rb +0 -99
- data/test/undefined_test.rb +0 -87
data/test/record_mode_test.rb
DELETED
@@ -1,158 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
#---
|
4
|
-
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
|
5
|
-
# All rights reserved.
|
6
|
-
|
7
|
-
# Permission is granted for use, copying, modification, distribution,
|
8
|
-
# and distribution of modified versions of this work as long as the
|
9
|
-
# above copyright notice is included.
|
10
|
-
#+++
|
11
|
-
|
12
|
-
require 'test/test_setup'
|
13
|
-
|
14
|
-
class TestRecordMode < Test::Unit::TestCase
|
15
|
-
include FlexMock::TestCase
|
16
|
-
|
17
|
-
def test_recording_mode_works
|
18
|
-
mock = flexmock("mock")
|
19
|
-
mock.should_expect do |recorder|
|
20
|
-
recorder.f { :answer }
|
21
|
-
end
|
22
|
-
assert_equal :answer, mock.f
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_arguments_are_passed_to_recording_mode_block
|
26
|
-
mock = flexmock("mock")
|
27
|
-
mock.should_expect do |recorder|
|
28
|
-
recorder.f(:arg) do |arg|
|
29
|
-
assert_equal :arg, arg
|
30
|
-
:answer
|
31
|
-
end
|
32
|
-
end
|
33
|
-
assert_equal :answer, mock.f(:arg)
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_recording_mode_handles_multiple_returns
|
37
|
-
FlexMock.use("mock") do |mock|
|
38
|
-
mock.should_expect do |r|
|
39
|
-
answers = [1, 2]
|
40
|
-
# HACK: The following lambda is needed in Ruby 1.9 to cause
|
41
|
-
# the answers to be properly bound in the following block.
|
42
|
-
lambda { }
|
43
|
-
r.f { answers.shift }
|
44
|
-
end
|
45
|
-
assert_equal 1, mock.f
|
46
|
-
assert_equal 2, mock.f
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_recording_mode_does_not_specify_order
|
51
|
-
FlexMock.use("mock") do |mock|
|
52
|
-
mock.should_expect do |r|
|
53
|
-
r.f { 1 }
|
54
|
-
r.g { 2 }
|
55
|
-
end
|
56
|
-
assert_equal 2, mock.g
|
57
|
-
assert_equal 1, mock.f
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_recording_mode_gets_block_args_too
|
62
|
-
mock = flexmock("mock")
|
63
|
-
mock.should_expect do |r|
|
64
|
-
r.f(1, Proc) { |arg, block|
|
65
|
-
assert_not_nil block
|
66
|
-
block.call
|
67
|
-
}
|
68
|
-
end
|
69
|
-
|
70
|
-
assert_equal :block_result, mock.f(1) { :block_result }
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_recording_mode_should_validate_args_with_equals
|
74
|
-
assert_mock_failure(:deep => true, :line => __LINE__+5) do
|
75
|
-
FlexMock.use("mock") do |mock|
|
76
|
-
mock.should_expect do |r|
|
77
|
-
r.f(1)
|
78
|
-
end
|
79
|
-
mock.f(2)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_recording_mode_should_allow_arg_contraint_validation
|
85
|
-
assert_mock_failure(:deep => true, :line => __LINE__+5) do
|
86
|
-
FlexMock.use("mock") do |mock|
|
87
|
-
mock.should_expect do |r|
|
88
|
-
r.f(1)
|
89
|
-
end
|
90
|
-
mock.f(2)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_recording_mode_should_handle_multiplicity_contraints
|
96
|
-
assert_mock_failure(:line => __LINE__+3) do
|
97
|
-
FlexMock.use("mock") do |mock|
|
98
|
-
mock.should_expect do |r|
|
99
|
-
r.f { :result }.once
|
100
|
-
end
|
101
|
-
mock.f
|
102
|
-
mock.f
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_strict_record_mode_requires_exact_argument_matches
|
108
|
-
assert_mock_failure(:deep => true, :line => __LINE__+6) do
|
109
|
-
FlexMock.use("mock") do |mock|
|
110
|
-
mock.should_expect do |rec|
|
111
|
-
rec.should_be_strict
|
112
|
-
rec.f(Integer)
|
113
|
-
end
|
114
|
-
mock.f(3)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_strict_record_mode_requires_exact_ordering
|
120
|
-
assert_mock_failure(:deep => true, :line => __LINE__+8) do
|
121
|
-
FlexMock.use("mock") do |mock|
|
122
|
-
mock.should_expect do |rec|
|
123
|
-
rec.should_be_strict
|
124
|
-
rec.f(1)
|
125
|
-
rec.f(2)
|
126
|
-
end
|
127
|
-
mock.f(2)
|
128
|
-
mock.f(1)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_strict_record_mode_requires_once
|
134
|
-
assert_mock_failure(:deep => true, :line => __LINE__+4) do
|
135
|
-
FlexMock.use("mock") do |mock|
|
136
|
-
mock.should_expect do |rec|
|
137
|
-
rec.should_be_strict
|
138
|
-
rec.f(1)
|
139
|
-
end
|
140
|
-
mock.f(1)
|
141
|
-
mock.f(1)
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
def test_strict_record_mode_can_not_fail
|
147
|
-
FlexMock.use("mock") do |mock|
|
148
|
-
mock.should_expect do |rec|
|
149
|
-
rec.should_be_strict
|
150
|
-
rec.f(Integer)
|
151
|
-
rec.f(2)
|
152
|
-
end
|
153
|
-
mock.f(Integer)
|
154
|
-
mock.f(2)
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
end
|
data/test/redirect_error.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'stringio'
|
2
|
-
|
3
|
-
class FlexMock
|
4
|
-
module RedirectError
|
5
|
-
def redirect_error
|
6
|
-
require 'stringio'
|
7
|
-
old_err = $stderr
|
8
|
-
$stderr = StringIO.new
|
9
|
-
yield
|
10
|
-
$stderr.string
|
11
|
-
ensure
|
12
|
-
$stderr = old_err
|
13
|
-
end
|
14
|
-
private :redirect_error
|
15
|
-
end
|
16
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
#---
|
4
|
-
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
|
5
|
-
# All rights reserved.
|
6
|
-
|
7
|
-
# Permission is granted for use, copying, modification, distribution,
|
8
|
-
# and distribution of modified versions of this work as long as the
|
9
|
-
# above copyright notice is included.
|
10
|
-
#+++
|
11
|
-
|
12
|
-
if defined?(RSpec)
|
13
|
-
RSpec.configure do |config|
|
14
|
-
config.mock_with :flexmock
|
15
|
-
end
|
16
|
-
else
|
17
|
-
Spec::Runner.configure do |config|
|
18
|
-
config.mock_with :flexmock
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "FlexMock in a RSpec example" do
|
23
|
-
specify "should be able to create a mock" do
|
24
|
-
m = flexmock()
|
25
|
-
end
|
26
|
-
|
27
|
-
specify "should have an error when a mock is not called" do
|
28
|
-
m = flexmock("Expectation Failured")
|
29
|
-
m.should_receive(:hi).with().once
|
30
|
-
lambda {
|
31
|
-
flexmock_verify
|
32
|
-
}.should raise_error(RSpec::Expectations::ExpectationNotMetError, /\bhi\b.*incorrect.*times/i)
|
33
|
-
end
|
34
|
-
|
35
|
-
specify "should be able to create a stub" do
|
36
|
-
s = "Hello World"
|
37
|
-
flexmock(:base, s).should_receive(:downcase).with().once.and_return("hello WORLD")
|
38
|
-
|
39
|
-
s.downcase.should == "hello WORLD"
|
40
|
-
end
|
41
|
-
|
42
|
-
specify "Should show an example failure" do
|
43
|
-
lambda {
|
44
|
-
1.should == 2
|
45
|
-
}.should raise_error(RSpec::Expectations::ExpectationNotMetError,
|
46
|
-
/expected: 2.*got: 1/m)
|
47
|
-
end
|
48
|
-
|
49
|
-
specify "Should show how mocks are displayed in error messages" do
|
50
|
-
m = flexmock("x")
|
51
|
-
lambda {
|
52
|
-
m.should == 2
|
53
|
-
}.should raise_error(RSpec::Expectations::ExpectationNotMetError, /got: <FlexMock:x>/)
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
@@ -1,207 +0,0 @@
|
|
1
|
-
require 'flexmock/rspec'
|
2
|
-
|
3
|
-
RSpec.configure do |config|
|
4
|
-
config.mock_with :flexmock
|
5
|
-
end
|
6
|
-
|
7
|
-
describe "Dog" do
|
8
|
-
class Dog
|
9
|
-
def wags(arg)
|
10
|
-
fail "SHOULD NOT BE CALLED"
|
11
|
-
end
|
12
|
-
|
13
|
-
def barks
|
14
|
-
fail "SHOULD NOT BE CALLED"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
let(:dog) { flexmock(:on, Dog) }
|
19
|
-
|
20
|
-
context "single calls with arguments" do
|
21
|
-
before do
|
22
|
-
dog.wags(:tail)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "accepts no with" do
|
26
|
-
dog.should have_received(:wags)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "accepts with restriction" do
|
30
|
-
dog.should have_received(:wags).with(:tail)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "accepts not methods called" do
|
34
|
-
dog.should_not have_received(:bark)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "rejects incorrect with restriction" do
|
38
|
-
should_fail(/^expected wag\(:foot\) to be received by <FlexMock:Dog Mock>/i) do
|
39
|
-
dog.should have_received(:wag).with(:foot)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
it "rejects not on correct matcher" do
|
44
|
-
should_fail(/^expected wags\(:tail\) to NOT be received by <FlexMock:Dog Mock>/i) do
|
45
|
-
dog.should_not have_received(:wags).with(:tail)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context "multiple calls with multiple arguments" do
|
51
|
-
before do
|
52
|
-
dog.wags(:tail)
|
53
|
-
dog.wags(:tail)
|
54
|
-
dog.wags(:tail)
|
55
|
-
dog.wags(:head, :tail)
|
56
|
-
dog.barks
|
57
|
-
dog.barks
|
58
|
-
end
|
59
|
-
|
60
|
-
it "accepts wags(:tail) multiple times" do
|
61
|
-
dog.should have_received(:wags).with(:tail).times(3)
|
62
|
-
end
|
63
|
-
|
64
|
-
it "rejects wags(:tail) wrong times value" do
|
65
|
-
should_fail(/^expected wags\(:tail\) to be received by <FlexMock:Dog Mock>/i) do
|
66
|
-
dog.should have_received(:wags).with(:tail).times(2)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
it "detects any_args" do
|
71
|
-
dog.should have_received(:wags).times(4)
|
72
|
-
end
|
73
|
-
|
74
|
-
it "accepts once" do
|
75
|
-
dog.should have_received(:wags).with(:head, :tail).once
|
76
|
-
end
|
77
|
-
|
78
|
-
it "rejects an incorrect once" do
|
79
|
-
should_fail(/^expected wags\(:tail\) to be received by <FlexMock:Dog Mock> once/i) do
|
80
|
-
dog.should have_received(:wags).with(:tail).once
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
it "accepts twice" do
|
85
|
-
dog.should have_received(:barks).twice
|
86
|
-
end
|
87
|
-
|
88
|
-
it "rejects an incorrect twice" do
|
89
|
-
should_fail(/^expected wags\(:tail\) to be received by <FlexMock:Dog Mock> twice/) do
|
90
|
-
dog.should have_received(:wags).with(:tail).twice
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
it "accepts never" do
|
95
|
-
dog.should have_received(:jump).never
|
96
|
-
end
|
97
|
-
|
98
|
-
it "rejects an incorrect never" do
|
99
|
-
should_fail(/^expected barks\(\) to be received by <FlexMock:Dog Mock> never/i) do
|
100
|
-
dog.should have_received(:barks).with().never
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
it "rejects an incorrect never" do
|
105
|
-
dog.should_not have_received(:jump)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
context "with additional validations" do
|
110
|
-
it "accepts when correct" do
|
111
|
-
dog.wags(:tail)
|
112
|
-
dog.should have_received(:wags).and { |arg| arg.should == :tail }
|
113
|
-
end
|
114
|
-
|
115
|
-
it "rejects when incorrect" do
|
116
|
-
dog.wags(:tail)
|
117
|
-
should_fail(/expected: :foot.*got: :tail/im) do
|
118
|
-
dog.should have_received(:wags).and { |arg| arg.should == :foot }
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
it "rejects on all calls" do
|
123
|
-
dog.wags(:foot)
|
124
|
-
dog.wags(:foot)
|
125
|
-
dog.wags(:tail)
|
126
|
-
should_fail(/expected: :foot.*got: :tail/im) do
|
127
|
-
dog.should have_received(:wags).and { |arg| arg.should == :foot }
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
it "runs the first additional block" do
|
132
|
-
dog.wags(:tail)
|
133
|
-
should_fail(/expected: :foot.*got: :tail/im) do
|
134
|
-
dog.should have_received(:wags).and { |args|
|
135
|
-
args.should == :foot
|
136
|
-
}.and { |args|
|
137
|
-
args.should == :tail
|
138
|
-
}
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
it "runs the second additional block" do
|
143
|
-
dog.wags(:tail)
|
144
|
-
should_fail(/expected: :foot.*got: :tail/im) do
|
145
|
-
dog.should have_received(:wags).and { |args|
|
146
|
-
args.should == :tail
|
147
|
-
}.and { |args|
|
148
|
-
args.should == :foot
|
149
|
-
}
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
context "with ordinal constraints" do
|
155
|
-
it "detects the first call" do
|
156
|
-
dog.wags(:tail)
|
157
|
-
dog.wags(:foot)
|
158
|
-
dog.should have_received(:wags).and { |arg| arg.should == :tail }.on(1)
|
159
|
-
end
|
160
|
-
|
161
|
-
it "detects the second call" do
|
162
|
-
dog.wags(:tail)
|
163
|
-
dog.wags(:foot)
|
164
|
-
dog.should have_received(:wags).and { |arg| arg.should == :foot }.on(2)
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
context "with blocks" do
|
169
|
-
before do
|
170
|
-
dog.wags { }
|
171
|
-
dog.barks
|
172
|
-
end
|
173
|
-
|
174
|
-
it "accepts wags with a block" do
|
175
|
-
dog.should have_received(:wags).with_a_block
|
176
|
-
dog.should have_received(:wags)
|
177
|
-
end
|
178
|
-
|
179
|
-
it "accepts barks without a block" do
|
180
|
-
dog.should have_received(:barks).without_a_block
|
181
|
-
dog.should have_received(:barks)
|
182
|
-
end
|
183
|
-
|
184
|
-
it "rejects wags without a block" do
|
185
|
-
should_fail(/without a block/) do
|
186
|
-
dog.should have_received(:wags).without_a_block
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
it "rejects barks with a block" do
|
191
|
-
should_fail(/with a block/) do
|
192
|
-
dog.should have_received(:barks).with_a_block
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
def should_fail(message_pattern)
|
198
|
-
failed = false
|
199
|
-
begin
|
200
|
-
yield
|
201
|
-
rescue RSpec::Expectations::ExpectationNotMetError => ex
|
202
|
-
failed = true
|
203
|
-
ex.message.should match(message_pattern)
|
204
|
-
end
|
205
|
-
RSpec::Expectations.fail_with "Expected block to fail with message #{message_pattern.inspect}, no failure detected" unless failed
|
206
|
-
end
|
207
|
-
end
|
data/test/samples_test.rb
DELETED
@@ -1,283 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
#---
|
4
|
-
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
|
5
|
-
# All rights reserved.
|
6
|
-
|
7
|
-
# Permission is granted for use, copying, modification, distribution,
|
8
|
-
# and distribution of modified versions of this work as long as the
|
9
|
-
# above copyright notice is included.
|
10
|
-
#+++
|
11
|
-
|
12
|
-
require 'test/test_setup'
|
13
|
-
|
14
|
-
# Sample FlexMock Usage.
|
15
|
-
|
16
|
-
class TestSamples < Test::Unit::TestCase
|
17
|
-
include FlexMock::TestCase
|
18
|
-
|
19
|
-
# This is a basic example where we setup a mock object to mimic an
|
20
|
-
# IO object. We know that the +count_lines+ method uses gets, so we
|
21
|
-
# tell the mock object to handle +gets+ by returning successive
|
22
|
-
# elements of an array (just as the real +gets+ returns successive
|
23
|
-
# elements of a file.
|
24
|
-
def test_file_io
|
25
|
-
mock_file = flexmock("file")
|
26
|
-
mock_file.should_receive(:gets).and_return("line 1", "line 2", nil)
|
27
|
-
assert_equal 2, count_lines(mock_file)
|
28
|
-
end
|
29
|
-
|
30
|
-
# Count the number of lines in a file. Used in the test_file_io
|
31
|
-
# test.
|
32
|
-
def count_lines(file)
|
33
|
-
n = 0
|
34
|
-
while file.gets
|
35
|
-
n += 1
|
36
|
-
end
|
37
|
-
n
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
class TestUndefined < Test::Unit::TestCase
|
43
|
-
include FlexMock::TestCase
|
44
|
-
|
45
|
-
def test_undefined_values
|
46
|
-
m = flexmock("mock")
|
47
|
-
m.should_receive(:divide_by).with(0).
|
48
|
-
and_return_undefined
|
49
|
-
assert_equal FlexMock.undefined, m.divide_by(0)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
class TestSimple < Test::Unit::TestCase
|
55
|
-
include FlexMock::TestCase
|
56
|
-
|
57
|
-
def test_simple_mock
|
58
|
-
m = flexmock(:pi => 3.1416, :e => 2.71)
|
59
|
-
assert_equal 3.1416, m.pi
|
60
|
-
assert_equal 2.71, m.e
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
class TestDog < Test::Unit::TestCase
|
65
|
-
include FlexMock::TestCase
|
66
|
-
|
67
|
-
def test_dog_wags
|
68
|
-
tail_mock = flexmock(:wag => :happy)
|
69
|
-
assert_equal :happy, tail_mock.wag
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
class Woofer
|
74
|
-
end
|
75
|
-
|
76
|
-
class Dog
|
77
|
-
def initialize
|
78
|
-
@woofer = Woofer.new
|
79
|
-
end
|
80
|
-
def bark
|
81
|
-
@woofer.woof
|
82
|
-
end
|
83
|
-
def wag
|
84
|
-
:happy
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
class TestDogBarking < Test::Unit::TestCase
|
89
|
-
include FlexMock::TestCase
|
90
|
-
|
91
|
-
# Setup the tests by mocking the +new+ method of
|
92
|
-
# Woofer and return a mock woofer.
|
93
|
-
def setup
|
94
|
-
@dog = Dog.new
|
95
|
-
flexmock(@dog, :bark => :grrr)
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_dog
|
99
|
-
assert_equal :grrr, @dog.bark # Mocked Method
|
100
|
-
assert_equal :happy, @dog.wag # Normal Method
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
class TestDogBarkingWithNewInstances < Test::Unit::TestCase
|
105
|
-
include FlexMock::TestCase
|
106
|
-
|
107
|
-
# Setup the tests by mocking Woofer to always
|
108
|
-
# return partial mocks.
|
109
|
-
def setup
|
110
|
-
flexmock(Woofer).new_instances.should_receive(:woof => :grrr)
|
111
|
-
end
|
112
|
-
|
113
|
-
def test_dog
|
114
|
-
assert_equal :grrr, Dog.new.bark # All dog objects
|
115
|
-
assert_equal :grrr, Dog.new.bark # are mocked.
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
class TestDefaults < Test::Unit::TestCase
|
120
|
-
include FlexMock::TestCase
|
121
|
-
|
122
|
-
def setup
|
123
|
-
@mock_dog = flexmock("Fido")
|
124
|
-
@mock_dog.should_receive(:tail => :a_tail, :bark => "woof").by_default
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_something_where_bark_must_be_called_once
|
128
|
-
@mock_dog.should_receive(:bark => "bow wow").once
|
129
|
-
assert_equal "bow wow", @mock_dog.bark
|
130
|
-
assert_equal :a_tail, @mock_dog.tail
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
class TestDemeter < Test::Unit::TestCase
|
135
|
-
include FlexMock::TestCase
|
136
|
-
def test_manual_mocking
|
137
|
-
# Manually mocking a Law of Demeter violation
|
138
|
-
cog = flexmock("cog")
|
139
|
-
cog.should_receive(:turn).once.and_return(:ok)
|
140
|
-
joint = flexmock("gear", :cog => cog)
|
141
|
-
axle = flexmock("axle", :universal_joint => joint)
|
142
|
-
chassis = flexmock("chassis", :axle => axle)
|
143
|
-
car = flexmock("car", :chassis => chassis)
|
144
|
-
|
145
|
-
# test code
|
146
|
-
assert_equal :ok, car.chassis.axle.universal_joint.cog.turn
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_demeter
|
150
|
-
car = flexmock("car")
|
151
|
-
car.should_receive( "chassis.axle.universal_joint.cog.turn" => :ok).once
|
152
|
-
|
153
|
-
# Test code
|
154
|
-
assert_equal :ok, car.chassis.axle.universal_joint.cog.turn
|
155
|
-
end
|
156
|
-
|
157
|
-
end
|
158
|
-
|
159
|
-
class TestDb < Test::Unit::TestCase
|
160
|
-
include FlexMock::TestCase
|
161
|
-
|
162
|
-
def test_db
|
163
|
-
db = flexmock('db')
|
164
|
-
db.should_receive(:query).and_return([1,2,3])
|
165
|
-
db.should_receive(:update).with(5).and_return(nil).once
|
166
|
-
|
167
|
-
# test code
|
168
|
-
assert_nil db.update(5)
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
|
173
|
-
class TestDb < Test::Unit::TestCase
|
174
|
-
include FlexMock::TestCase
|
175
|
-
|
176
|
-
def test_query_and_update
|
177
|
-
db = flexmock('db')
|
178
|
-
db.should_receive(:query).and_return([1,2,3]).ordered
|
179
|
-
db.should_receive(:update).and_return(nil).ordered
|
180
|
-
# test code here
|
181
|
-
assert_raises(assertion_failed_error) do
|
182
|
-
db.update
|
183
|
-
db.query
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_ordered_queries
|
188
|
-
db = flexmock('db')
|
189
|
-
db.should_receive(:startup).once.ordered
|
190
|
-
db.should_receive(:query).with("CPWR").and_return(12.3).
|
191
|
-
once.ordered(:queries)
|
192
|
-
db.should_receive(:query).with("MSFT").and_return(10.0).
|
193
|
-
once.ordered(:queries)
|
194
|
-
db.should_receive(:query).with(/^....$/).and_return(3.3).
|
195
|
-
at_least.once.ordered(:queries)
|
196
|
-
db.should_receive(:finish).once.ordered
|
197
|
-
# test code here
|
198
|
-
db.startup
|
199
|
-
db.query("CPWR")
|
200
|
-
db.query("MSFT")
|
201
|
-
db.query("asdf")
|
202
|
-
db.finish
|
203
|
-
end
|
204
|
-
|
205
|
-
def test_ordered_queries_in_record_mode
|
206
|
-
db = flexmock('db')
|
207
|
-
db.should_expect do |rec|
|
208
|
-
rec.startup.once.ordered
|
209
|
-
rec.query("CPWR") { 12.3 }.once.ordered(:queries)
|
210
|
-
rec.query("MSFT") { 10.0 }.once.ordered(:queries)
|
211
|
-
rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
|
212
|
-
rec.finish.once.ordered
|
213
|
-
end
|
214
|
-
# test code here using +db+.
|
215
|
-
db.startup
|
216
|
-
db.query("CPWR")
|
217
|
-
db.query("MSFT")
|
218
|
-
db.query("asdf")
|
219
|
-
db.finish
|
220
|
-
end
|
221
|
-
|
222
|
-
def known_good_way_to_build_xml(builder)
|
223
|
-
builder.html
|
224
|
-
end
|
225
|
-
|
226
|
-
def new_way_to_build_xml(builder)
|
227
|
-
known_good_way_to_build_xml(builder)
|
228
|
-
end
|
229
|
-
|
230
|
-
def test_build_xml
|
231
|
-
builder = flexmock('builder')
|
232
|
-
builder.should_expect do |rec|
|
233
|
-
rec.should_be_strict
|
234
|
-
known_good_way_to_build_xml(rec) # record the messages
|
235
|
-
end
|
236
|
-
new_way_to_build_xml(builder) # compare to new way
|
237
|
-
end
|
238
|
-
|
239
|
-
end
|
240
|
-
|
241
|
-
class TestMoreSamples < Test::Unit::TestCase
|
242
|
-
include FlexMock::TestCase
|
243
|
-
|
244
|
-
def test_multiple_gets
|
245
|
-
file = flexmock('file')
|
246
|
-
file.should_receive(:gets).with_no_args.
|
247
|
-
and_return("line 1\n", "line 2\n")
|
248
|
-
# test code here
|
249
|
-
assert_equal "line 1\n", file.gets
|
250
|
-
assert_equal "line 2\n", file.gets
|
251
|
-
end
|
252
|
-
|
253
|
-
def test_an_important_message
|
254
|
-
m = flexmock('m')
|
255
|
-
m.should_receive(:an_important_message).and_return(1).once
|
256
|
-
m.should_ignore_missing
|
257
|
-
# test code here
|
258
|
-
assert_equal 1, m.an_important_message
|
259
|
-
assert_equal FlexMock.undefined, m.other
|
260
|
-
end
|
261
|
-
|
262
|
-
class QuoteService
|
263
|
-
end
|
264
|
-
|
265
|
-
class Portfolio
|
266
|
-
def initialize
|
267
|
-
@quote_service = QuoteService.new
|
268
|
-
end
|
269
|
-
def value
|
270
|
-
@quote_service.quote
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
def test_portfolio_value
|
275
|
-
flexmock(QuoteService).new_instances do |m|
|
276
|
-
m.should_receive(:quote).and_return(100)
|
277
|
-
end
|
278
|
-
port = Portfolio.new
|
279
|
-
value = port.value # Portfolio calls QuoteService.quote
|
280
|
-
assert_equal 100, value
|
281
|
-
end
|
282
|
-
|
283
|
-
end
|