rspec-spies 1.2.9
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +116 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/rspec-spies.rb +53 -0
- data/spec/rspec-spies_spec.rb +33 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +75 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Joshua Nichols
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
= rspec-spies
|
2
|
+
|
3
|
+
Behold, the Test Spies pattern! http://xunitpatterns.com/Test%20Spy.html
|
4
|
+
|
5
|
+
Some other mocking frameworks support this. In particular, rr, notamock, and jferris's fork of mocha. It'd be great to see support for it in rspec's mocking library as well.
|
6
|
+
|
7
|
+
Why not just switch to another framework? Well, because migration is a huge hassle when you have a sizeable number of specs, in part to subtle varations in the framework. Even something like notamock that was meant to work with rspec out of the box... has its differences. Therefore, using the existing rspec mocking framework, with a small enhancements, is pretty idyllic.
|
8
|
+
|
9
|
+
In general, I think it should work by doing your usual stub!, followed running the code under test, and then verifying with a matcher. The matcher should probably be in line with the syntax of mocking with should_receive. Here's a short example:
|
10
|
+
|
11
|
+
|
12
|
+
describe "index" do
|
13
|
+
before do
|
14
|
+
@post = stub_model(Post)
|
15
|
+
Post.stub!(:find).and_return(@post)
|
16
|
+
get :show, :id => @post.id
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should find post by id" do
|
20
|
+
Post.should have_received(:find).with(@post.id)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
To get started, you just need to:
|
25
|
+
|
26
|
+
* Run: gem install rspec-spies
|
27
|
+
* Update spec_helper to include this line: require 'rspec-spies'
|
28
|
+
|
29
|
+
|
30
|
+
== Before and after test spies
|
31
|
+
|
32
|
+
If you think this simple case is redudant, consider this extended example without using test spies (note: these are using shoulda's rspec matchers):
|
33
|
+
|
34
|
+
describe "show" do
|
35
|
+
before do
|
36
|
+
@post = stub_model(Post)
|
37
|
+
# always stub find, so most of the specifications will work
|
38
|
+
# alternatively, this could be in each 'it' block that doesn't have a should_receive
|
39
|
+
Post.stub!(:find).and_return(@post)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should respond with success" do
|
43
|
+
get :show, :id => @post.id
|
44
|
+
|
45
|
+
controller.should respond_with(:success)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should render show template" do
|
49
|
+
get :show, :id => @post.id
|
50
|
+
controller.should render_template(:show)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should find the post by id" do
|
54
|
+
Post.should_receive(:find).with(@post.id).and_return(@post)
|
55
|
+
# note we have to specify the return value again
|
56
|
+
# also, this expectation is set BEFORE we actually do anything
|
57
|
+
get :show, :id => @post.id
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should assign post" do
|
61
|
+
get :show, :id
|
62
|
+
|
63
|
+
controller.should assign_to(:post).with(@post)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
The problems here:
|
69
|
+
|
70
|
+
* Running get in each it block. This is because we want to be able to use a mock expectation (ie @Post.should_receive) in one of the specifications. The should_receive could be in the setup instead of @stub!@, but if find is not called, it'll make all the specs fail, instead of just the one expecting it to be called.
|
71
|
+
* We're stubbing it once in the setup, and then mocking the same method in a later. This means we have to specify the return value both in the setup, so other specs work, and then again in the 'it' block with the mocking so that one will work.
|
72
|
+
|
73
|
+
Contrast this with using test spies for this same situation:
|
74
|
+
|
75
|
+
describe "show" do
|
76
|
+
before do
|
77
|
+
@post = stub_model(Post)
|
78
|
+
Post.stub!(:find).and_return(@post)
|
79
|
+
|
80
|
+
get :show, :id => @post.id
|
81
|
+
end
|
82
|
+
|
83
|
+
it { should respond_with(:success) }
|
84
|
+
|
85
|
+
it { should render_template(:show) }
|
86
|
+
|
87
|
+
it { should assign_to(:post).with(@post) }
|
88
|
+
|
89
|
+
it "should find the post by id" do
|
90
|
+
Post.should have_received(:find).with(@post.id)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
Note here that we only do a get once, and that we only have to specify the return value of @find once in the setup block.
|
95
|
+
|
96
|
+
When I came on board the project I'm currently on (written by people who I want to think were pretty good at rspec), it was entirely in the style of the first example whenever mocking was used. This is a pretty simple case, but you could imagine it getting out of control if you are mocking more than just a single method. As a result, it took me a lot longer than it should have to fully understand what was going on, and it took longer to add more specs than it should have.
|
97
|
+
|
98
|
+
After writing up the test spies, I've been rewriting the specs when I return to them to write them in the style of the second example, and it definitely feels a lot better.
|
99
|
+
|
100
|
+
Lastly, I'd just want to make it clear that I'm not suggesting we stop using the first style. I just want the option of using test spies. The way this patch is written, I don't see it interfering with the existing style.
|
101
|
+
|
102
|
+
|
103
|
+
== Note on Patches/Pull Requests
|
104
|
+
|
105
|
+
* Fork the project.
|
106
|
+
* Make your feature addition or bug fix.
|
107
|
+
* Add tests for it. This is important so I don't break it in a
|
108
|
+
future version unintentionally.
|
109
|
+
* Commit, do not mess with rakefile, version, or history.
|
110
|
+
(if you want to have your own version, that is fine but
|
111
|
+
bump version in a commit by itself I can ignore when I pull)
|
112
|
+
* Send me a pull request. Bonus points for topic branches.
|
113
|
+
|
114
|
+
== Copyright
|
115
|
+
|
116
|
+
Copyright (c) 2009 Joshua Nichols. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "rspec-spies"
|
8
|
+
gem.summary = %Q{rspec has gone without tests spies. no more!}
|
9
|
+
gem.description = %Q{test spies, for rspec}
|
10
|
+
gem.email = "josh@technicalpickles.com"
|
11
|
+
gem.homepage = "http://github.com/technicalpickles/rspec-spies"
|
12
|
+
gem.authors = ["Joshua Nichols"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "rspec-spies #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.2.9
|
data/lib/rspec-spies.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec/mocks/proxy'
|
2
|
+
Spec::Mocks::Proxy.class_eval do
|
3
|
+
# override defining stubs to record the message was called.
|
4
|
+
# there's only one line difference from upstream rspec, but can't change it without fully overriding
|
5
|
+
|
6
|
+
def define_expected_method(sym)
|
7
|
+
unless @proxied_methods.include?(sym)
|
8
|
+
visibility_string = "#{visibility(sym)} :#{sym}"
|
9
|
+
if target_responds_to?(sym)
|
10
|
+
munged_sym = munge(sym)
|
11
|
+
target_metaclass.instance_eval do
|
12
|
+
alias_method munged_sym, sym if method_defined?(sym)
|
13
|
+
end
|
14
|
+
@proxied_methods << sym
|
15
|
+
end
|
16
|
+
target_metaclass.class_eval(<<-EOF, __FILE__, __LINE__)
|
17
|
+
def #{sym}(*args, &block)
|
18
|
+
__mock_proxy.record_message_received(:#{sym}, args, block) # this is the only line changed by rspec-spies in this method
|
19
|
+
__mock_proxy.message_received :#{sym}, *args, &block
|
20
|
+
end
|
21
|
+
#{visibility_string}
|
22
|
+
EOF
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'spec/matchers'
|
28
|
+
Spec::Matchers.module_eval do
|
29
|
+
def have_received(sym, &block)
|
30
|
+
Spec::Matchers::Matcher.new :have_received, sym, @args, block do |sym, args, block|
|
31
|
+
match do |actual|
|
32
|
+
actual.received_message?(sym, *@args, &block)
|
33
|
+
end
|
34
|
+
|
35
|
+
failure_message_for_should do |actual|
|
36
|
+
"expected #{actual.inspect} to have received #{sym.inspect} with #{args.inspect}"
|
37
|
+
end
|
38
|
+
|
39
|
+
failure_message_for_should_not do |actual|
|
40
|
+
"expected #{actual.inspect} to not have received #{sym.inspect} with #{args.inspect}, but did"
|
41
|
+
end
|
42
|
+
|
43
|
+
description do
|
44
|
+
"to have received #{sym.inspect} with #{args.inspect}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def with(*args)
|
48
|
+
@args = args
|
49
|
+
self
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
module Spec
|
4
|
+
module Matchers
|
5
|
+
describe "[object.should] have_received(method, *args)" do
|
6
|
+
before do
|
7
|
+
@object = String.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "does match if method is called with correct args" do
|
11
|
+
@object.stub!(:slice)
|
12
|
+
@object.slice(5)
|
13
|
+
|
14
|
+
have_received(:slice).with(5).matches?(@object).should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "does not match if method is called with incorrect args" do
|
18
|
+
@object.stub!(:slice)
|
19
|
+
@object.slice(3)
|
20
|
+
|
21
|
+
have_received(:slice).with(5).matches?(@object).should be_false
|
22
|
+
end
|
23
|
+
|
24
|
+
it "does not match if method is not called" do
|
25
|
+
@object.stub!(:slice)
|
26
|
+
|
27
|
+
have_received(:slice).with(5).matches?(@object).should be_false
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-spies
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.9
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshua Nichols
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-05 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
description: test spies, for rspec
|
26
|
+
email: josh@technicalpickles.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/rspec-spies.rb
|
42
|
+
- spec/rspec-spies_spec.rb
|
43
|
+
- spec/spec.opts
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/technicalpickles/rspec-spies
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: rspec has gone without tests spies. no more!
|
73
|
+
test_files:
|
74
|
+
- spec/rspec-spies_spec.rb
|
75
|
+
- spec/spec_helper.rb
|