fakes-rspec 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +8 -0
- data/Gemfile +3 -0
- data/Guardfile +5 -0
- data/LICENSE +22 -0
- data/README.md +167 -0
- data/Rakefile +1 -0
- data/fakes-rspec.gemspec +26 -0
- data/lib/fakes-rspec.rb +16 -0
- data/lib/fakes_rspec/block_criteria.rb +12 -0
- data/lib/fakes_rspec/matcher.rb +46 -0
- data/lib/fakes_rspec/nullo_specification.rb +10 -0
- data/lib/fakes_rspec/occurances.rb +20 -0
- data/lib/fakes_rspec/received_criteria.rb +14 -0
- data/lib/fakes_rspec/received_occurances_criteria.rb +16 -0
- data/lib/fakes_rspec/version.rb +5 -0
- metadata +18 -3
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jean-Paul S. Boodhoo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
#fakes-rspec
|
2
|
+
|
3
|
+
This is a library to aid in the usage of [fakes](http://github.com/developwithpassion/fakes) when using [RSpec](https://github.com/rspec/rspec). It adds a bunch of convienience methods and matchers to aid in the usage of the heavily AAA style isolation library.
|
4
|
+
|
5
|
+
##Installation
|
6
|
+
```bash
|
7
|
+
gem install fakes-rspec
|
8
|
+
```
|
9
|
+
Or (preferably using bundler), in your gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
source "http://rubygems.org"
|
13
|
+
gem 'fakes-rspec'
|
14
|
+
```
|
15
|
+
|
16
|
+
When you install the gem it will install the rspec gem also, so you will be immediately ready to go.
|
17
|
+
|
18
|
+
##Usage
|
19
|
+
|
20
|
+
##Creating a fake
|
21
|
+
|
22
|
+
###Using a let block
|
23
|
+
```ruby
|
24
|
+
describe "Some Feature" do
|
25
|
+
let(:my_fake){fake}
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
###Inline
|
30
|
+
```ruby
|
31
|
+
describe "Some Feature" do
|
32
|
+
it "should be able to create a fake" do
|
33
|
+
item = fake
|
34
|
+
end
|
35
|
+
end
|
36
|
+
```
|
37
|
+
##Configuring a fake with return values for calls
|
38
|
+
|
39
|
+
###Irrespective of arguments:
|
40
|
+
```ruby
|
41
|
+
it "should be able to setup a fakes return values" do
|
42
|
+
the_fake = fake
|
43
|
+
fake.stub(:hello).and_return("World")
|
44
|
+
|
45
|
+
fake.hello.should == "World"
|
46
|
+
fake.hello("There").should == "World"
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
###Caring about arguments:
|
51
|
+
```ruby
|
52
|
+
it "should be able to setup a fakes return values" do
|
53
|
+
the_fake = fake
|
54
|
+
fake.stub(:hello).with("There").and_return("World")
|
55
|
+
fake.stub(:hello).with("You").and_return("Again")
|
56
|
+
|
57
|
+
fake.stub(:hello).and_return("Does Not Matter") # when you use the catch_all, make sure that it is the last step used for a particular method (as above)
|
58
|
+
|
59
|
+
fake.hello("There").should == "World"
|
60
|
+
fake.hello("You").should == "Again"
|
61
|
+
fake.hello.should == "Does Not Matter"
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
##Determining whether a call was made
|
66
|
+
|
67
|
+
One of the big strengths of this library compared to some of the other ruby isolation libraries is the ability to let you make assertions against the fake after the [subject] has run its code. The following examples demonstrate some typical usage scenarios:
|
68
|
+
|
69
|
+
###Irrespective of arguments:
|
70
|
+
```ruby
|
71
|
+
it "should be able to determine if a call was made on a fake" do
|
72
|
+
the_fake = fake
|
73
|
+
fake.hello("World")
|
74
|
+
|
75
|
+
fake.should have_received(:hello) #true
|
76
|
+
end
|
77
|
+
```
|
78
|
+
###With a specific set of arguments:
|
79
|
+
```ruby
|
80
|
+
it "should be able to determine if a call was made on a fake" do
|
81
|
+
the_fake = fake
|
82
|
+
fake.hello("World")
|
83
|
+
|
84
|
+
fake.should have_received(:hello,"World") #true
|
85
|
+
fake.should have_received(:hello,"Other") #false
|
86
|
+
end
|
87
|
+
```
|
88
|
+
Remember, that because it is just a matcher, to negate the matcher you can use the should_not qualifier to do the opposite:
|
89
|
+
|
90
|
+
###Determine whether a call was not made with a specific set of arguments:
|
91
|
+
```ruby
|
92
|
+
it "should be able to determine if a call was not made on a fake" do
|
93
|
+
the_fake = fake
|
94
|
+
fake.hello("World")
|
95
|
+
|
96
|
+
fake.should_not have_received(:hello,"Other") #true
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
##Determining that a call was made a certain number of times
|
101
|
+
|
102
|
+
###Irrespective of arguments:
|
103
|
+
```ruby
|
104
|
+
it "should be able to determine if a call was made on a fake" do
|
105
|
+
the_fake = fake
|
106
|
+
fake.hello("World")
|
107
|
+
|
108
|
+
fake.should have_received(:hello).once #true
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
###Caring about arguments:
|
113
|
+
```ruby
|
114
|
+
it "should be able to determine if a call was made on a fake" do
|
115
|
+
the_fake = fake
|
116
|
+
fake.hello("World")
|
117
|
+
|
118
|
+
fake.should have_received(:hello,"World").once #true
|
119
|
+
fake.should have_received(:hello,"Earth").once #false
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
Remember, that because it is just a matcher, to negate the matcher you can use the should_not qualifier to do the opposite:
|
124
|
+
|
125
|
+
###Determine whether a call was not made a specific number of times with a specific set of arguments:
|
126
|
+
```ruby
|
127
|
+
it "should be able to determine if a call was made on a fake" do
|
128
|
+
the_fake = fake
|
129
|
+
fake.hello("World")
|
130
|
+
|
131
|
+
fake.should_not have_received(:hello,"World").twice #true
|
132
|
+
fake.should_not have_received(:hello).twice #true
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
After calling have_received, you can specify occurences using one of the following methods:
|
137
|
+
|
138
|
+
* once
|
139
|
+
* twice
|
140
|
+
* at_least_once
|
141
|
+
* at_least_twice
|
142
|
+
* at_most_once
|
143
|
+
* at_most_twice
|
144
|
+
* at_least(times)
|
145
|
+
* at_most(times)
|
146
|
+
* exactly(times)
|
147
|
+
* occurs(match_block) - Where match_block is a proc/lambda that matches the signature lambda{|number| bool}
|
148
|
+
|
149
|
+
An example of using the occurs method would be as follows:
|
150
|
+
|
151
|
+
###Determine whether a call was made between a certain number of times
|
152
|
+
```ruby
|
153
|
+
it "should be able to determine if a call was made on a fake" do
|
154
|
+
the_fake = fake
|
155
|
+
fake.hello("World")
|
156
|
+
fake.hello("Again")
|
157
|
+
|
158
|
+
fake.should have_received(,:hello).occurs(lamdba{|number| (1..3) === number}) #true
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
##Contributing
|
163
|
+
|
164
|
+
Feel free to fork this codebase and submit any pull requests you think would be useful. When you submit a pull request, make sure to include an accompanying test for the feature.
|
165
|
+
|
166
|
+
|
167
|
+
[Develop With Passion®](http://www.developwithpassion.com)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/fakes-rspec.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib",__FILE__)
|
3
|
+
require "fakes_rspec/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "fakes-rspec"
|
7
|
+
s.version = Fakes::RSpec::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Develop With Passion®"]
|
10
|
+
s.email = ["open_source@developwithpassion.com"]
|
11
|
+
s.homepage = "http://www.developwithpassion.com"
|
12
|
+
s.summary = %q{Utility functions for develowithpassion_fakes when working with rSpec}
|
13
|
+
s.description = %q{Faking library that allows inspection of received calls after they have been made. Also supports tracking calls with multiple argument sets.}
|
14
|
+
s.rubyforge_project = "fakes-rspec"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency "rake"
|
21
|
+
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "guard"
|
23
|
+
s.add_development_dependency "guard-rspec"
|
24
|
+
s.add_runtime_dependency "fakes"
|
25
|
+
s.add_runtime_dependency "rspec"
|
26
|
+
end
|
data/lib/fakes-rspec.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'fakes'
|
2
|
+
require 'rspec'
|
3
|
+
require 'singleton'
|
4
|
+
|
5
|
+
require 'fakes_rspec/block_criteria'
|
6
|
+
require 'fakes_rspec/matcher'
|
7
|
+
require 'fakes_rspec/nullo_specification'
|
8
|
+
require 'fakes_rspec/occurances'
|
9
|
+
require 'fakes_rspec/received_occurances_criteria'
|
10
|
+
require 'fakes_rspec/received_criteria'
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.after(:each) do
|
14
|
+
reset_fake_classes
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module RSpec
|
2
|
+
def self.create_received_criteria_from(the_call)
|
3
|
+
return Fakes::RSpec::ReceivedCriteria.new(the_call)
|
4
|
+
end
|
5
|
+
def self.create_received_occurences_criteria_from(the_call,occurence)
|
6
|
+
return Fakes::RSpec::ReceivedOccurencesCriteria.new(create_received_criteria_from(the_call),the_call,occurence)
|
7
|
+
end
|
8
|
+
|
9
|
+
Matchers.define :have_received do|symbol,*args|
|
10
|
+
@occurence_spec = Fakes::RSpec::NulloSpecification.instance
|
11
|
+
|
12
|
+
chain :once do
|
13
|
+
@occurence_spec = Fakes::RSpec::Occurances.exact(1)
|
14
|
+
end
|
15
|
+
chain :twice do
|
16
|
+
@occurence_spec = Fakes::RSpec::Occurances.exact(2)
|
17
|
+
end
|
18
|
+
chain :at_least_once do
|
19
|
+
@occurence_spec = Fakes::RSpec::Occurances.at_least(1)
|
20
|
+
end
|
21
|
+
chain :at_least_twice do
|
22
|
+
@occurence_spec = Fakes::RSpec::Occurances.at_least(2)
|
23
|
+
end
|
24
|
+
chain :at_most_once do
|
25
|
+
@occurence_spec = Fakes::RSpec::Occurances.at_most(1)
|
26
|
+
end
|
27
|
+
chain :at_most_twice do
|
28
|
+
@occurence_spec = Fakes::RSpec::Occurances.at_most(2)
|
29
|
+
end
|
30
|
+
chain :at_least do|times|
|
31
|
+
@occurence_spec = Fakes::RSpec::Occurances.at_least(times)
|
32
|
+
end
|
33
|
+
chain :at_most do|times|
|
34
|
+
@occurence_spec = Fakes::RSpec::Occurances.at_most(times)
|
35
|
+
end
|
36
|
+
chain :exactly do|times|
|
37
|
+
@occurence_spec = Fakes::RSpec::Occurances.exact(times)
|
38
|
+
end
|
39
|
+
chain :occurs do|the_proc|
|
40
|
+
@occurence_spec = Fakes::RSpec::Occurances.from_block(the_proc)
|
41
|
+
end
|
42
|
+
match do|the_fake|
|
43
|
+
RSpec.create_received_occurences_criteria_from(the_fake.received(symbol),@occurence_spec).is_satisfied_by(*args)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Fakes
|
2
|
+
module RSpec
|
3
|
+
class Occurances
|
4
|
+
class << self
|
5
|
+
def from_block(the_block)
|
6
|
+
return BlockCriteria.new(the_block)
|
7
|
+
end
|
8
|
+
def exact(times)
|
9
|
+
return from_block(lambda{|occurances| return occurances == times})
|
10
|
+
end
|
11
|
+
def at_least(times)
|
12
|
+
return from_block(lambda { |occurences| return occurences >= times})
|
13
|
+
end
|
14
|
+
def at_most(times)
|
15
|
+
return from_block(lambda { |occurences| return occurences <= times})
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Fakes
|
2
|
+
module RSpec
|
3
|
+
class ReceivedCriteria
|
4
|
+
def initialize(the_call)
|
5
|
+
@the_call = the_call
|
6
|
+
end
|
7
|
+
|
8
|
+
def is_satisfied_by(*args)
|
9
|
+
return false if @the_call == nil
|
10
|
+
return args.count == 0 ? true : @the_call.called_with(*args) != nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Fakes
|
2
|
+
module RSpec
|
3
|
+
class ReceivedOccurencesCriteria
|
4
|
+
def initialize(received_criteria,the_call,occurence)
|
5
|
+
@received_criteria = received_criteria
|
6
|
+
@the_call = the_call
|
7
|
+
@occurence = occurence
|
8
|
+
end
|
9
|
+
|
10
|
+
def is_satisfied_by(*args)
|
11
|
+
return @received_criteria.is_satisfied_by(*args) &&
|
12
|
+
@occurence.is_satisfied_by((args.count == 0 ? @the_call.total_times_called : @the_call.called_with(*args).times_called))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fakes-rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -115,6 +115,21 @@ executables: []
|
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
|
+
- .gitignore
|
119
|
+
- Gemfile
|
120
|
+
- Guardfile
|
121
|
+
- LICENSE
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- fakes-rspec.gemspec
|
125
|
+
- lib/fakes-rspec.rb
|
126
|
+
- lib/fakes_rspec/block_criteria.rb
|
127
|
+
- lib/fakes_rspec/matcher.rb
|
128
|
+
- lib/fakes_rspec/nullo_specification.rb
|
129
|
+
- lib/fakes_rspec/occurances.rb
|
130
|
+
- lib/fakes_rspec/received_criteria.rb
|
131
|
+
- lib/fakes_rspec/received_occurances_criteria.rb
|
132
|
+
- lib/fakes_rspec/version.rb
|
118
133
|
- spec/spec_helper.rb
|
119
134
|
- spec/specs/block_criteria_spec.rb
|
120
135
|
- spec/specs/nullo_specification_spec.rb
|
@@ -136,7 +151,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
136
151
|
version: '0'
|
137
152
|
segments:
|
138
153
|
- 0
|
139
|
-
hash: -
|
154
|
+
hash: -3560833146225028214
|
140
155
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
156
|
none: false
|
142
157
|
requirements:
|
@@ -145,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
160
|
version: '0'
|
146
161
|
segments:
|
147
162
|
- 0
|
148
|
-
hash: -
|
163
|
+
hash: -3560833146225028214
|
149
164
|
requirements: []
|
150
165
|
rubyforge_project: fakes-rspec
|
151
166
|
rubygems_version: 1.8.25
|