developwithpassion_fakes 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm 1.9.2@developwithpassion.fakes --create
1
+ rvm 1.9.3@developwithpassion_fakes --create
data/README.md ADDED
@@ -0,0 +1,229 @@
1
+ #developwithpassion_fakes
2
+
3
+ This is a really simple library to aid in AAA style testing. The primary driver for using this is to be able to make assertions on method calls to collaborators in actual assertions and not as part of setup. It is meant to be used to complement the current testing framework that you are using to aid in the creation of interaction based tests.
4
+
5
+ Here is a simple example
6
+
7
+ ```ruby
8
+ class SomeClass
9
+ def initialize(collaborator)
10
+ @collaborator = collaborator
11
+ end
12
+ def run()
13
+ @collaborator.send_message("Hi")
14
+ end
15
+ end
16
+
17
+ describe SomeClass do
18
+ context "when run" do
19
+ let(:collaborator){DevelopWithPassion::Fakes::Fake.new}
20
+ let(:sut){SomeClass.new(collaborator)}
21
+
22
+ before(:each) do
23
+ sut.run
24
+ end
25
+
26
+ it "should trigger its collaborator with the correct message" do
27
+ collaborator.received(:send_message).called_with("Hi").should_not be_nil
28
+ end
29
+ end
30
+ end
31
+ ```
32
+
33
+ ##Creating a new fake
34
+
35
+ To create a new fake, simply leverage the fake method that is mixed into the Kernel module.
36
+
37
+ ```ruby
38
+ require 'developwithpassion_fakes'
39
+
40
+ item = fake
41
+ ```
42
+
43
+ ##Specifying the behaviour of a fake
44
+
45
+ When scaffolding fake return values, the library behaves almost identically to the way RSpec stubs work.
46
+
47
+ ###Setup a method to return a value for a particular set of arguments
48
+ ```ruby
49
+ collaborator = fake
50
+
51
+ collaborator.stub(:name_of_method).with(arg1,arg2,arg3).and_return(return_value)
52
+ ```
53
+
54
+ ###Setup a method to return a value regardless of the arguments it is called with
55
+ ```ruby
56
+ collaborator = fake
57
+
58
+ #long handed way
59
+ collaborator.stub(:name_of_method).ignore_arg.and_return(return_value)
60
+
61
+ #preferred way
62
+ collaborator.stub(:name_of_method).and_return(return_value)
63
+ ```
64
+
65
+ ###Setup different return values for different argument sets
66
+ ```ruby
67
+ collaborator = fake
68
+
69
+ #Setup a return value for 1
70
+ collaborator.stub(:method).with(1).and_return(first_return_value)
71
+
72
+ #Setup a return value for 2
73
+ collaborator.stub(:method).with(2).and_return(second_return_value)
74
+
75
+ #Setup a return value when called with everything else
76
+ #if you are going to use this, make sure it is used after
77
+ #setting up return values for specific arguments
78
+ collaborator.stub(:method).and_return(value_to_return_with_arguments_other_than_1_and_2)
79
+ ```
80
+
81
+ ##Verifying calls made to the fake
82
+
83
+
84
+ ###Verifying when a call was made
85
+
86
+ The primary purpose of the library is to help you in doing interaction style testing in a AAA style. Assume the following class is one you would like to test:
87
+
88
+ ```ruby
89
+ class ItemToTest
90
+ def initialize(collaborator)
91
+ @collaborator = collaborator
92
+ end
93
+
94
+ def run
95
+ @collaborator.send_message("Hello World")
96
+ end
97
+ end
98
+ ```
99
+
100
+ ItemToTest is supposed to leverage its collaborator and calls its send_message method with the argument "Hello World". To verify this using AAA style, interaction testing you can do the following (I am using rspec, but you can use this with any testing library you wish):
101
+
102
+ ```ruby
103
+ describe ItemToTest do
104
+ context "when run" do
105
+ let(:collaborator){fake}
106
+ let(:sut){ItemToTest.new(collaborator)}
107
+
108
+ #I typically use a before block to specifically trigger the method that I am testing, so it cleanly
109
+ #separates it from the assertions I will make later
110
+ before(:each) do
111
+ sut.run
112
+ end
113
+
114
+ it "should trigger its collaborator with the correct message" do
115
+ collaborator.received(:send_message).called_with("Hello World").should_not be_nil
116
+ end
117
+ end
118
+ end
119
+ ```
120
+ From the example above, you can see that we created the fake and did not need to scaffold it with any behaviour.
121
+
122
+ ```ruby
123
+ let(:collaborator){fake}
124
+ ```
125
+
126
+ You can also see that we are create our System Under Test (sut) and provide it the collaborator:
127
+
128
+ ```ruby
129
+ let(:sut){ItemToTest.new(collaborator)}
130
+ ```
131
+
132
+ We then proceed to invoke the method on the component we are testing
133
+
134
+ ```ruby
135
+ before(:each) do
136
+ sut.run
137
+ end
138
+ ```
139
+
140
+ Last but not least, we verify that our collaborator was invoked and with the right arguments:
141
+
142
+ ```ruby
143
+ it "should trigger its collaborator with the correct message" do
144
+ collaborator.received(:send_message).called_with("Hello World").should_not be_nil
145
+ end
146
+ ```
147
+
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 create a test utility method that allows you to leverage your testing frameworks assertion library to make the above assertion more terse. The
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
+ ```
160
+
161
+ Using the above utility method turns the previous assertion:
162
+
163
+ ```ruby
164
+ collaborator.received(:send_message).called_with("Hello World").should_not be_nil
165
+ ```
166
+
167
+ To this:
168
+
169
+ ```ruby
170
+ collaborator.should have_received(:send_message,"Hello World")
171
+ ```
172
+
173
+ ###Verifying that a call should not have been made
174
+
175
+ Currently verifying that a call was not made does not take the arguments into consideration. It just ensures that no calls to a particular named method were made. Here is an example:
176
+
177
+ ```ruby
178
+ class FirstCollaborator
179
+ def send_message(message)
180
+ end
181
+ end
182
+ class SecondCollaborator
183
+ def send_message(message)
184
+ end
185
+ end
186
+
187
+ class SomeItem
188
+ def initialize
189
+ @first = FirstCollaborator.new
190
+ @second = SecondCollaborator.new
191
+ end
192
+
193
+ def first_behaviour
194
+ @first.send_message("Hello")
195
+ end
196
+
197
+ def second_behaviour
198
+ @second.send_message("World")
199
+ end
200
+ end
201
+
202
+ describe SomeItem do
203
+ context "when run" do
204
+ let(:first){fake}
205
+ let(:second){fake}
206
+
207
+ before(:each) do
208
+ FirstCollaborator.stub(:new).and_return(first)
209
+ SecondCollaborator.stub(:new).and_return(second)
210
+ @sut = SomeItem.new
211
+ end
212
+
213
+ before(:each) do
214
+ @sut.first_behaviour
215
+ end
216
+
217
+ it "should trigger its collaborator with the correct message" do
218
+ first.should have_received(:send_message,"Hello")
219
+ end
220
+
221
+ it "should not trigger its second collaborator" do
222
+ #again, here would be another option to use a convienience test utility method
223
+ second.never_received?(:send_message).should be_true
224
+ end
225
+ end
226
+ end
227
+ ```
228
+
229
+ As you can see, in this test we want to verify that one collaborator was triggered and the other not.
@@ -1,5 +1,5 @@
1
1
  module DevelopWithPassion
2
2
  module Fakes
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -3,3 +3,9 @@ require 'developwithpassion_fakes/arg_set'
3
3
  require 'developwithpassion_fakes/fake'
4
4
  require 'developwithpassion_fakes/ignore_set'
5
5
  require 'developwithpassion_fakes/method_stub'
6
+
7
+ module Kernel
8
+ def fake
9
+ return DevelopWithPassion::Fakes::Fake.new
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ describe Kernel do
2
+ it "should be able to create a new fake using the kernel exposed method" do
3
+ fake.class.should == DevelopWithPassion::Fakes::Fake
4
+ end
5
+ end
6
+
@@ -55,7 +55,7 @@ module DevelopWithPassion
55
55
  end
56
56
  context "and it has the specified argument set" do
57
57
  let(:arguments){[1]}
58
- let(:arg_set){DummyArgSet.new(2)}
58
+ let(:arg_set){DummyArgSet.new}
59
59
  before (:each) do
60
60
  arg_sets.push(arg_set)
61
61
  arg_set.stub(:matches?).and_return(true)
@@ -74,7 +74,7 @@ module DevelopWithPassion
74
74
  end
75
75
  context "and it does not have the specified argument set" do
76
76
  let(:arguments){[1]}
77
- let(:arg_set){DummyArgSet.new(2)}
77
+ let(:arg_set){DummyArgSet.new}
78
78
  before (:each) do
79
79
  sut.stub(:ignore_arg).and_return(arg_set)
80
80
  arg_set.stub(:return_value).and_return(2)
@@ -105,7 +105,7 @@ module DevelopWithPassion
105
105
  let(:arguments){[1]}
106
106
 
107
107
  context "and one of its argument sets was called with the set of arguments" do
108
- let(:arg_set){DummyArgSet.new(2)}
108
+ let(:arg_set){DummyArgSet.new}
109
109
  before (:each) do
110
110
  arg_sets.push(arg_set)
111
111
  arg_set.stub(:was_called_with?).with(arguments).and_return(true)
@@ -132,8 +132,8 @@ module DevelopWithPassion
132
132
  context "when verifying whether it was called a certain number of times" do
133
133
  let(:arg_sets){[]}
134
134
  let(:sut){MethodStub.new(arg_sets)}
135
- let(:arg_set){DummyArgSet.new(2)}
136
- let(:arg_set_2){DummyArgSet.new(2)}
135
+ let(:arg_set){DummyArgSet.new}
136
+ let(:arg_set_2){DummyArgSet.new}
137
137
 
138
138
  before (:each) do
139
139
  arg_sets.push(arg_set)
metadata CHANGED
@@ -1,41 +1,39 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: developwithpassion_fakes
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
4
5
  prerelease:
5
- version: 0.0.1
6
6
  platform: ruby
7
- authors:
8
- - "Develop With Passion\xC2\xAE"
7
+ authors:
8
+ - Develop With Passion®
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-02-10 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-02-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: rspec
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70258249562040 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
24
22
  type: :development
25
- version_requirements: *id001
26
- description: Faking library that allows inspection of received calls after they have been made. Also supports tracking calls with multiple argument sets.
27
- email:
23
+ prerelease: false
24
+ version_requirements: *70258249562040
25
+ description: Faking library that allows inspection of received calls after they have
26
+ been made. Also supports tracking calls with multiple argument sets.
27
+ email:
28
28
  - open_source@developwithpassion.com
29
29
  executables: []
30
-
31
30
  extensions: []
32
-
33
31
  extra_rdoc_files: []
34
-
35
- files:
32
+ files:
36
33
  - .gitignore
37
34
  - .rvmrc
38
35
  - Gemfile
36
+ - README.md
39
37
  - Rakefile
40
38
  - developwithpassion_fakes.gemspec
41
39
  - lib/developwithpassion_fakes.rb
@@ -50,38 +48,37 @@ files:
50
48
  - spec/specs/arg_set_spec.rb
51
49
  - spec/specs/fake_spec.rb
52
50
  - spec/specs/ignore_set_spec.rb
51
+ - spec/specs/kernel_spec.rb
53
52
  - spec/specs/method_stub_spec.rb
54
53
  homepage: http://www.developwithpassion.com
55
54
  licenses: []
56
-
57
55
  post_install_message:
58
56
  rdoc_options: []
59
-
60
- require_paths:
57
+ require_paths:
61
58
  - lib
62
- required_ruby_version: !ruby/object:Gem::Requirement
59
+ required_ruby_version: !ruby/object:Gem::Requirement
63
60
  none: false
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: "0"
68
- required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
66
  none: false
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- version: "0"
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
74
71
  requirements: []
75
-
76
72
  rubyforge_project: developwithpassion_fakes
77
- rubygems_version: 1.8.11
73
+ rubygems_version: 1.8.15
78
74
  signing_key:
79
75
  specification_version: 3
80
76
  summary: Simple faking library
81
- test_files:
77
+ test_files:
82
78
  - spec/spec_helper.rb
83
79
  - spec/specs/arg_behaviour_spec.rb
84
80
  - spec/specs/arg_set_spec.rb
85
81
  - spec/specs/fake_spec.rb
86
82
  - spec/specs/ignore_set_spec.rb
83
+ - spec/specs/kernel_spec.rb
87
84
  - spec/specs/method_stub_spec.rb