rspec-spy 0.0.2 → 0.0.3
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/README.md +61 -14
- data/lib/rspec/spy/mock_methods.rb +8 -0
- data/lib/rspec/spy/version.rb +1 -1
- data/spec/rspec_spy_spec.rb +13 -0
- metadata +6 -6
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# RSpec::Spy
|
2
2
|
|
3
3
|
This gem allows you to write mock expectations in an AAA (Arrange-Act-Assert) fashion
|
4
|
-
with RSpec::Mocks. It does this by allowing you to declare examples within a spy block,
|
4
|
+
with RSpec::Mocks. It does this by allowing you to declare examples within a `spy` block,
|
5
5
|
which is effectively executed before everything else. Here's a simple example:
|
6
6
|
|
7
7
|
``` ruby
|
@@ -38,16 +38,14 @@ Or install it yourself as:
|
|
38
38
|
|
39
39
|
$ gem install rspec-spy
|
40
40
|
|
41
|
-
## Usage
|
42
|
-
|
43
41
|
Add to your spec_helper.rb:
|
44
42
|
|
45
43
|
``` ruby
|
46
44
|
require 'rspec-spy'
|
47
45
|
```
|
48
46
|
|
49
|
-
If you want to be warned when using should_receive outside of spy blocks (recommended)
|
50
|
-
add this to your spec_helper.rb
|
47
|
+
If you want to be warned when using `should_receive` outside of `spy` blocks (recommended)
|
48
|
+
add this to your `spec_helper.rb`:
|
51
49
|
|
52
50
|
``` ruby
|
53
51
|
# Require should_receive and should_not_receive to be inside spy blocks
|
@@ -55,9 +53,11 @@ add this to your spec_helper.rb:
|
|
55
53
|
RSpec::Spy.strict_mode = true
|
56
54
|
```
|
57
55
|
|
58
|
-
|
59
|
-
|
60
|
-
|
56
|
+
## Usage
|
57
|
+
|
58
|
+
Just put your spy expectations within `spy` blocks in your specs instead of in `before` blocks.
|
59
|
+
You should be able to use all of the functionality from rspec-mocks that you're
|
60
|
+
used to, including spying on class methods.
|
61
61
|
|
62
62
|
``` ruby
|
63
63
|
spy do
|
@@ -75,19 +75,66 @@ end
|
|
75
75
|
## Warnings
|
76
76
|
|
77
77
|
* This is a hack, you'll want to make sure everyone on your team is aware of the behavior
|
78
|
-
and these warnings.
|
79
|
-
* You should probably avoid instance vars
|
80
|
-
in before blocks and use them in spy blocks. Remember, spy blocks actually happen before
|
81
|
-
before blocks.
|
78
|
+
and these warnings.
|
79
|
+
* You should probably avoid instance vars and stick to `let`, it gets confusing because you cannot set them
|
80
|
+
in `before` blocks and use them in `spy` blocks. Remember, `spy` blocks actually happen before
|
81
|
+
`before` blocks. Example (see the example at the beginning of the readme for the right way):
|
82
|
+
|
83
|
+
``` ruby
|
84
|
+
describe "what not to do" do
|
85
|
+
before do
|
86
|
+
@collaborator = stub.as_null_object
|
87
|
+
@collaborator.message
|
88
|
+
end
|
89
|
+
|
90
|
+
# These will fail because @collaborator is nil because this happens
|
91
|
+
# before the above before block
|
92
|
+
spy do
|
93
|
+
it "should receive a message" do
|
94
|
+
@collaborator.should_receive :message
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should not receive other_message" do
|
98
|
+
@collaborator.should_not_receive :other_message
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
```
|
82
103
|
* If your tests depend on the method you are spying on returning something then you'll
|
83
|
-
need to use `and_return` in your spy block and if you have normal examples you'll also
|
84
|
-
need to stub it. Yes, this is annoying, but that's how rspec-mocks works
|
104
|
+
need to use `and_return` in your `spy` block and if you have normal examples you'll also
|
105
|
+
need to `stub` it. Yes, this is annoying, but that's how rspec-mocks works and it's one
|
106
|
+
of the many reasons you shoudln't mock when the return value matters (just `stub`). Example:
|
107
|
+
|
108
|
+
``` ruby
|
109
|
+
describe "stubbing and mocking at the same time" do
|
110
|
+
let(:collaborator) { stub.as_null_object }
|
111
|
+
before do
|
112
|
+
collaborator.stub(:message) { 5 }
|
113
|
+
@result = collaborator.message
|
114
|
+
|
115
|
+
# This will fail unless you use and_return
|
116
|
+
@result.should == 5
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should return 5" do
|
120
|
+
# This will fail unless you stub in before
|
121
|
+
@result.should == 5
|
122
|
+
end
|
123
|
+
|
124
|
+
spy do
|
125
|
+
it "should receive a message" do
|
126
|
+
collaborator.should_receive(:message).and_return(5)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
```
|
85
131
|
|
86
132
|
## Alternatives
|
87
133
|
|
88
134
|
* [matahari](https://github.com/mortice/matahari)
|
89
135
|
* [bourne](https://github.com/thoughtbot/bourne)
|
90
136
|
* [rspec-spies](https://github.com/technicalpickles/rspec-spies)
|
137
|
+
* [gimme](https://github.com/searls/gimme)
|
91
138
|
|
92
139
|
## Contributing
|
93
140
|
|
@@ -3,11 +3,13 @@ require 'rspec/mocks'
|
|
3
3
|
RSpec::Mocks::Methods.class_eval do
|
4
4
|
def should_receive_with_spy_check(message, opts={}, &block)
|
5
5
|
spy_check(:should_receive)
|
6
|
+
nil_check(:should_receive)
|
6
7
|
__mock_proxy.add_message_expectation(opts[:expected_from] || caller(1)[0], message.to_sym, opts, &block)
|
7
8
|
end
|
8
9
|
|
9
10
|
def should_not_receive_with_spy_check(message, &block)
|
10
11
|
spy_check(:should_not_receive)
|
12
|
+
nil_check(:should_not_receive)
|
11
13
|
__mock_proxy.add_negative_message_expectation(caller(1)[0], message.to_sym, &block)
|
12
14
|
end
|
13
15
|
|
@@ -23,5 +25,11 @@ RSpec::Mocks::Methods.class_eval do
|
|
23
25
|
return if RSpec::Spy.ok_to_spy?
|
24
26
|
raise "#{method} should not be used outside of a spy block. Please put it in a spy block or use #{method}!."
|
25
27
|
end
|
28
|
+
|
29
|
+
def nil_check(method)
|
30
|
+
return if RSpec::Mocks::Proxy.allow_message_expectations_on_nil?
|
31
|
+
return unless nil?
|
32
|
+
raise "You set an expectation on nil, maybe you're using an @instance_var? If you want to do this, use allow_message_expectations_on_nil."
|
33
|
+
end
|
26
34
|
end
|
27
35
|
|
data/lib/rspec/spy/version.rb
CHANGED
data/spec/rspec_spy_spec.rb
CHANGED
@@ -86,3 +86,16 @@ describe RSpec::Spy, "shorthand" do
|
|
86
86
|
collaborator.should_receive :message
|
87
87
|
end
|
88
88
|
end
|
89
|
+
|
90
|
+
describe RSpec::Spy, "nil warning" do
|
91
|
+
spy.it "should warn me if I try to should_receive on nil" do
|
92
|
+
lambda { @unknown.should_receive :message }.should raise_error
|
93
|
+
lambda { @unknown.should_not_receive :message }.should raise_error
|
94
|
+
end
|
95
|
+
|
96
|
+
spy.it "should not warn me if I allow message expectations on nil" do
|
97
|
+
RSpec::Mocks::Proxy.allow_message_expectations_on_nil
|
98
|
+
lambda { @unknown.should_not_receive :message }.should_not raise_error
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-spy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-04-03 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70355483304080 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '2.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70355483304080
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec-mocks
|
27
|
-
requirement: &
|
27
|
+
requirement: &70355483303580 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '2.0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70355483303580
|
36
36
|
description: Enables AAA testing for rspec-mock
|
37
37
|
email:
|
38
38
|
- aaronjensen@gmail.com
|
@@ -79,7 +79,7 @@ rubyforge_project:
|
|
79
79
|
rubygems_version: 1.8.17
|
80
80
|
signing_key:
|
81
81
|
specification_version: 3
|
82
|
-
summary: rspec-spy-0.0.
|
82
|
+
summary: rspec-spy-0.0.3
|
83
83
|
test_files:
|
84
84
|
- spec/rspec_spy_spec.rb
|
85
85
|
- spec/spec_helper.rb
|