not_a_mock 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,242 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'not_a_mock'
3
+
4
+ describe "A stubbed method replacing an existing instance method" do
5
+
6
+ before do
7
+ @object = "Hello, world!"
8
+ @object.stub_method(:length => 42)
9
+ end
10
+
11
+ it "should return the stubbed result" do
12
+ @object.length.should == 42
13
+ end
14
+
15
+ it "should return the original result after stubbing is removed" do
16
+ @object.unstub_method(:length)
17
+ @object.length.should == 13
18
+ end
19
+
20
+ it "should return the original result after a reset" do
21
+ NotAMock::Stubber.instance.reset
22
+ @object.length.should == 13
23
+ end
24
+
25
+ it "should return the new result if re-stubbed" do
26
+ @object.stub_method(:length => 24)
27
+ @object.length.should == 24
28
+ end
29
+
30
+ it "should record a call to the stubbed method" do
31
+ @object.length
32
+ NotAMock::CallRecorder.instance.calls.should include(:object => @object, :method => :length, :args => [], :result => 42)
33
+ end
34
+
35
+ it "should return a call to the stubbed method if re-stubbed" do
36
+ @object.stub_method(:length => 24)
37
+ @object.length
38
+ NotAMock::CallRecorder.instance.calls.should include(:object => @object, :method => :length, :args => [], :result => 24)
39
+ end
40
+
41
+ after do
42
+ NotAMock::CallRecorder.instance.reset
43
+ NotAMock::Stubber.instance.reset
44
+ end
45
+
46
+ end
47
+
48
+ describe "A stubbed method with no existing instance method" do
49
+
50
+ before do
51
+ @object = "Hello, world!"
52
+ @object.stub_method(:blah => 42)
53
+ end
54
+
55
+ it "should return the stubbed result" do
56
+ @object.blah.should == 42
57
+ end
58
+
59
+ it "should raise a NoMethodError when the method is called after stubbing is removed" do
60
+ @object.unstub_method(:blah)
61
+ lambda { @object.blah }.should raise_error(NoMethodError)
62
+ end
63
+
64
+ it "should raise a NoMethodError when the method is called after all stubbing is removed" do
65
+ NotAMock::Stubber.instance.reset
66
+ lambda { @object.blah }.should raise_error(NoMethodError)
67
+ end
68
+
69
+ it "should record a call to the stubbed method" do
70
+ @object.blah
71
+ NotAMock::CallRecorder.instance.calls.should include(:object => @object, :method => :blah, :args => [], :result => 42)
72
+ end
73
+
74
+ after do
75
+ NotAMock::CallRecorder.instance.reset
76
+ NotAMock::Stubber.instance.reset
77
+ end
78
+
79
+ end
80
+
81
+ describe "A stubbed class method" do
82
+
83
+ before do
84
+ Time.stub_method(:now => 42)
85
+ end
86
+
87
+ it "should return the stubbed result" do
88
+ Time.now.should == 42
89
+ end
90
+
91
+ it "should return the original result after stubbing is removed" do
92
+ Time.unstub_method(:now)
93
+ Time.now.should_not == 42
94
+ end
95
+
96
+ it "should return the original result after a reset" do
97
+ NotAMock::Stubber.instance.reset
98
+ Time.now.should_not == 42
99
+ end
100
+
101
+ after do
102
+ NotAMock::CallRecorder.instance.reset
103
+ NotAMock::Stubber.instance.reset
104
+ end
105
+
106
+ end
107
+
108
+ describe "A stubbed private method" do
109
+ before do
110
+ class Privateer
111
+ private
112
+
113
+ def self.stubbed
114
+ false
115
+ end
116
+ end
117
+
118
+ Privateer.stub_method(:stubbed => true)
119
+ end
120
+
121
+ it "should return the stubbed result" do
122
+ Privateer.send(:stubbed).should be_true
123
+ end
124
+
125
+ it "should return the original result after stubbing is removed" do
126
+ Privateer.unstub_method(:stubbed)
127
+ Privateer.send(:stubbed).should be_false
128
+ end
129
+
130
+ it "should return the original result after a reset" do
131
+ NotAMock::Stubber.instance.reset
132
+ Privateer.send(:stubbed).should be_false
133
+ end
134
+
135
+ after do
136
+ NotAMock::CallRecorder.instance.reset
137
+ NotAMock::Stubber.instance.reset
138
+ end
139
+ end
140
+
141
+ describe "A stubbed protected method" do
142
+ before :each do
143
+ class Protector
144
+ protected
145
+
146
+ def self.stubbed
147
+ false
148
+ end
149
+ end
150
+
151
+ Protector.stub_method(:stubbed => true)
152
+ end
153
+
154
+ it "should return the stubbed result" do
155
+ Protector.send(:stubbed).should be_true
156
+ end
157
+
158
+ it "should return the original result after stubbing is removed" do
159
+ Protector.unstub_method(:stubbed)
160
+ Protector.send(:stubbed).should be_false
161
+ end
162
+
163
+ it "should return the original result after a reset" do
164
+ NotAMock::Stubber.instance.reset
165
+ Protector.send(:stubbed).should be_false
166
+ end
167
+
168
+ after do
169
+ NotAMock::CallRecorder.instance.reset
170
+ NotAMock::Stubber.instance.reset
171
+ end
172
+ end
173
+
174
+ describe "A method stubbed with a block" do
175
+
176
+ before do
177
+ @object = "Hello, world!"
178
+ @object.stub_method(:length) do |*args|
179
+ args.reverse
180
+ end
181
+ end
182
+
183
+ it "should return the result of the block" do
184
+ @object.length(1, 2, 3).should == [3, 2, 1]
185
+ end
186
+
187
+ after do
188
+ NotAMock::CallRecorder.instance.reset
189
+ NotAMock::Stubber.instance.reset
190
+ end
191
+
192
+ end
193
+
194
+ describe "A method stubbed to raise an exception" do
195
+
196
+ before do
197
+ @object = "Hello, world!"
198
+ @object.stub_method_to_raise(:length => ArgumentError)
199
+ end
200
+
201
+ it "should raise the exception when called" do
202
+ lambda { @object.length }.should raise_error(ArgumentError)
203
+ end
204
+
205
+ it "should return the original result after stubbing is removed" do
206
+ @object.unstub_method(:length)
207
+ @object.length.should == 13
208
+ end
209
+
210
+ after do
211
+ NotAMock::CallRecorder.instance.reset
212
+ NotAMock::Stubber.instance.reset
213
+ end
214
+
215
+ end
216
+
217
+ describe "Object#stub_method" do
218
+
219
+ it "should stub a method with a name ending in '?'" do
220
+ @object = "Hello, world!"
221
+ @object.stub_method(:is_great? => true)
222
+ @object.is_great?.should be_true
223
+ end
224
+
225
+ it "should stub the []= method" do
226
+ @object = Array.new
227
+ @object.stub_method(:[]= => nil)
228
+ @object[0] = 7
229
+ @object.length.should == 0
230
+ end
231
+
232
+ it "should raise an ArgumentError if called with something other than a symbol or hash" do
233
+ @object = "Hello, world!"
234
+ lambda { @object.stub_method(7) }.should raise_error(ArgumentError)
235
+ end
236
+
237
+ after do
238
+ NotAMock::CallRecorder.instance.reset
239
+ NotAMock::Stubber.instance.reset
240
+ end
241
+
242
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: not_a_mock
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pete Yandell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-08 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: pete@notahat.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - TODO
25
+ files:
26
+ - .gitignore
27
+ - MIT-LICENSE
28
+ - README.rdoc
29
+ - Rakefile
30
+ - TODO
31
+ - VERSION
32
+ - lib/not_a_mock.rb
33
+ - lib/not_a_mock/active_record_extensions.rb
34
+ - lib/not_a_mock/argument_constraint_extensions.rb
35
+ - lib/not_a_mock/call_recorder.rb
36
+ - lib/not_a_mock/matchers.rb
37
+ - lib/not_a_mock/matchers/anything_matcher.rb
38
+ - lib/not_a_mock/matchers/args_matcher.rb
39
+ - lib/not_a_mock/matchers/call_matcher.rb
40
+ - lib/not_a_mock/matchers/method_matcher.rb
41
+ - lib/not_a_mock/matchers/result_matcher.rb
42
+ - lib/not_a_mock/matchers/times_matcher.rb
43
+ - lib/not_a_mock/object_extensions.rb
44
+ - lib/not_a_mock/rspec_mock_framework_adapter.rb
45
+ - lib/not_a_mock/stub.rb
46
+ - lib/not_a_mock/stubber.rb
47
+ - spec/call_recording_spec.rb
48
+ - spec/matchers_spec.rb
49
+ - spec/stub_active_record_spec.rb
50
+ - spec/stub_instance_spec.rb
51
+ - spec/stub_method_spec.rb
52
+ has_rdoc: true
53
+ homepage: http://notahat.com/not_a_mock
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --charset=UTF-8
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.5
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: A cleaner and DRYer alternative to mocking and stubbing with RSpec
80
+ test_files:
81
+ - spec/call_recording_spec.rb
82
+ - spec/matchers_spec.rb
83
+ - spec/stub_active_record_spec.rb
84
+ - spec/stub_instance_spec.rb
85
+ - spec/stub_method_spec.rb