fakes 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +12 -16
- data/lib/core/arg_matching/matches.rb +7 -7
- data/lib/core/version.rb +1 -1
- data/spec/specs/arg_matching_scenarios_spec.rb +1 -1
- data/spec/specs/matches_spec.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -72,10 +72,16 @@ collaborator.stub(:method).with(1).and_return(first_return_value)
|
|
72
72
|
#Setup a return value for 2
|
73
73
|
collaborator.stub(:method).with(2).and_return(second_return_value)
|
74
74
|
|
75
|
+
|
75
76
|
#Setup a return value when called with everything else
|
76
77
|
#if you are going to use this, make sure it is used after
|
77
78
|
#setting up return values for specific arguments
|
78
79
|
collaborator.stub(:method).and_return(value_to_return_with_arguments_other_than_1_and_2)
|
80
|
+
|
81
|
+
|
82
|
+
#Setup a return value for any number greater than 2 - this makes use of the argument matching syntax described [here](http://blog.developwithpassion.com/2012/06/09/fakes-and-in-turn-fakes-rspec-new-feature-arbitrary-argument-matching-for-setup-and-verification-of-calls/)
|
83
|
+
|
84
|
+
collaborator.stub(:method).with(arg_match.greater_than(2)).and_return(second_return_value)
|
79
85
|
```
|
80
86
|
|
81
87
|
##Verifying calls made to the fake
|
@@ -145,26 +151,14 @@ it "should trigger its collaborator with the correct message" do
|
|
145
151
|
end
|
146
152
|
```
|
147
153
|
|
148
|
-
The nice thing is we can make the assertions after the fact, as opposed to needing to do them as part of setup, which I find is a much more natural way to read things, when you need to do this style of test. Notice that the called_with method return a method_invocation that will be nil if the call was not received. My recommendation would be to
|
149
|
-
following rspec sample demonstrates:
|
150
|
-
|
151
|
-
```ruby
|
152
|
-
module RSpec
|
153
|
-
Matchers.define :have_received do|symbol,*args|
|
154
|
-
match do|fake|
|
155
|
-
fake.received(symbol).called_with(*args) != nil
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
159
|
-
```
|
154
|
+
The nice thing is we can make the assertions after the fact, as opposed to needing to do them as part of setup, which I find is a much more natural way to read things, when you need to do this style of test. Notice that the called_with method return a method_invocation that will be nil if the call was not received. My recommendation would be to use the [fakes-rspec](http://github.com/developwithpassion/fakes-rpsec) library (if you are using rspec) which gives you access to a predefined matcher that you can use as follows:
|
160
155
|
|
161
|
-
Using the above utility method turns the previous assertion:
|
162
156
|
|
163
157
|
```ruby
|
164
158
|
collaborator.received(:send_message).called_with("Hello World").should_not be_nil
|
165
159
|
```
|
166
160
|
|
167
|
-
|
161
|
+
Can be done in fakes-rspec by doing this:
|
168
162
|
|
169
163
|
```ruby
|
170
164
|
collaborator.should have_received(:send_message,"Hello World")
|
@@ -172,7 +166,7 @@ collaborator.should have_received(:send_message,"Hello World")
|
|
172
166
|
|
173
167
|
###Verifying that a call should not have been made
|
174
168
|
|
175
|
-
|
169
|
+
Verifying that a call was not made can be done with or without arguments:
|
176
170
|
|
177
171
|
```ruby
|
178
172
|
class FirstCollaborator
|
@@ -221,9 +215,11 @@ describe SomeItem do
|
|
221
215
|
it "should not trigger its second collaborator" do
|
222
216
|
#again, here would be another option to use a convienience test utility method
|
223
217
|
second.never_received?(:send_message).should be_true
|
218
|
+
#with fakes-rspec it looks like this
|
219
|
+
second.should_not have_received(:send_message)
|
224
220
|
end
|
225
221
|
end
|
226
222
|
end
|
227
223
|
```
|
228
224
|
|
229
|
-
As you can see, in this test we want to verify that one collaborator was triggered and the other not.
|
225
|
+
As you can see, in this test we want to verify that one collaborator was triggered and the other not. If you cared about specifying arguments it should not have been called with, you can do the same as a regular verification.
|
@@ -2,31 +2,31 @@ module Fakes
|
|
2
2
|
class Matches
|
3
3
|
class << self
|
4
4
|
def not_nil
|
5
|
-
condition
|
5
|
+
condition{|item| item != nil}
|
6
6
|
end
|
7
7
|
|
8
8
|
def nil
|
9
|
-
condition
|
9
|
+
condition{|item| item == nil}
|
10
10
|
end
|
11
11
|
|
12
12
|
def any
|
13
|
-
condition
|
13
|
+
condition{|ignored| true}
|
14
14
|
end
|
15
15
|
|
16
16
|
def greater_than(value)
|
17
|
-
condition
|
17
|
+
condition{|number| number > value}
|
18
18
|
end
|
19
19
|
|
20
20
|
|
21
21
|
def in_range(range)
|
22
|
-
condition
|
22
|
+
condition{|item| range === item}
|
23
23
|
end
|
24
24
|
|
25
25
|
def regex(regex)
|
26
|
-
condition
|
26
|
+
condition{|string| regex =~ string}
|
27
27
|
end
|
28
28
|
|
29
|
-
def condition(conditional_block)
|
29
|
+
def condition(&conditional_block)
|
30
30
|
return BlockArgMatcher.new(conditional_block)
|
31
31
|
end
|
32
32
|
end
|
data/lib/core/version.rb
CHANGED
@@ -39,7 +39,7 @@ module Fakes
|
|
39
39
|
|
40
40
|
fake.hello(7)
|
41
41
|
|
42
|
-
fake.received(:hello).called_with(Matches.condition
|
42
|
+
fake.received(:hello).called_with(Matches.condition{|item| item < 10}).should_not be_nil
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should be able to intercept by mixing regular arguments with matchers" do
|
data/spec/specs/matches_spec.rb
CHANGED
@@ -34,7 +34,7 @@ module Fakes
|
|
34
34
|
end
|
35
35
|
|
36
36
|
it "should be able to create a lambda based matcher" do
|
37
|
-
match = Matches.condition
|
37
|
+
match = Matches.condition{|item| item > 3}
|
38
38
|
match.matches?(2).should be_false
|
39
39
|
match.matches?(7).should be_true
|
40
40
|
end
|