better_receive 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/README.md +2 -0
- data/lib/better_receive.rb +4 -0
- data/lib/better_receive/base.rb +14 -0
- data/lib/better_receive/mock.rb +24 -0
- data/lib/better_receive/stub.rb +1 -0
- data/lib/better_receive/version.rb +1 -1
- data/spec/lib/better_receive/mock_spec.rb +111 -0
- data/spec/lib/better_receive/stub_spec.rb +12 -3
- metadata +11 -19
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2374b6397328e21d63f479e68e74a2dfdc843960
|
4
|
+
data.tar.gz: c4e755a254de947b2fe8bed0bcca4e390177a513
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c755925be275f776cb9e7bfc86372785b96fb96a348583b2d03252d4e4b4771f13ab7189652a6e498f7de6cbdc7937d87406c129a71b324fc4b8a00289412b8
|
7
|
+
data.tar.gz: 2692f614cf4c2803c7f24e91fa9fc92ec545c5e54432aadc9a943934155ac54900bcb0c16ee9cb25c0c82121811ff00d853bd4d786c281288fd1e84abc211085
|
data/.gitignore
CHANGED
data/README.md
CHANGED
data/lib/better_receive.rb
CHANGED
@@ -8,6 +8,10 @@ module BetterReceive
|
|
8
8
|
Mock.new(self).assert_with(*args, &block)
|
9
9
|
end
|
10
10
|
|
11
|
+
def better_not_receive(*args, &block)
|
12
|
+
Mock.new(self).assert_negative_with(*args, &block)
|
13
|
+
end
|
14
|
+
|
11
15
|
def better_stub(*args, &block)
|
12
16
|
Stub.new(self).assert_with(*args, &block)
|
13
17
|
end
|
data/lib/better_receive/base.rb
CHANGED
@@ -27,6 +27,11 @@ module BetterReceive
|
|
27
27
|
any_instance_add_expectation selector, &block
|
28
28
|
end
|
29
29
|
|
30
|
+
def any_instance_better_not_expect(selector, options, &block)
|
31
|
+
any_instance_should_respond_to selector
|
32
|
+
any_instance_add_negative_expectation selector, &block
|
33
|
+
end
|
34
|
+
|
30
35
|
def any_instance_should_respond_to(selector)
|
31
36
|
klass = subject.instance_variable_get(:@klass)
|
32
37
|
unless klass.method_defined?(selector)
|
@@ -43,5 +48,14 @@ module BetterReceive
|
|
43
48
|
subject.message_chains.add(selector, expectation_chain(selector, &block))
|
44
49
|
end
|
45
50
|
|
51
|
+
def any_instance_add_negative_expectation(selector, &block)
|
52
|
+
RSpec::Mocks::space.add(subject)
|
53
|
+
|
54
|
+
subject.instance_variable_set(:@expectation_set, true)
|
55
|
+
subject.send(:observe!, selector)
|
56
|
+
|
57
|
+
subject.message_chains.add(selector, negative_expectation_chain(selector, &block))
|
58
|
+
end
|
59
|
+
|
46
60
|
end
|
47
61
|
end
|
data/lib/better_receive/mock.rb
CHANGED
@@ -2,6 +2,7 @@ module BetterReceive
|
|
2
2
|
class Mock < Base
|
3
3
|
|
4
4
|
def assert_with(selector, options={}, &block)
|
5
|
+
selector = selector.to_sym
|
5
6
|
if subject_is_any_instance?
|
6
7
|
any_instance_better_expect(selector, options, &block)
|
7
8
|
else
|
@@ -10,6 +11,16 @@ module BetterReceive
|
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
14
|
+
def assert_negative_with(selector, options={}, &block)
|
15
|
+
selector = selector.to_sym
|
16
|
+
if subject_is_any_instance?
|
17
|
+
any_instance_better_not_expect(selector, options, &block)
|
18
|
+
else
|
19
|
+
subject.should respond_to selector
|
20
|
+
negative_mock_subject_method(selector, options, &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
13
24
|
|
14
25
|
private
|
15
26
|
|
@@ -18,6 +29,11 @@ module BetterReceive
|
|
18
29
|
subject_mock_proxy.add_message_expectation(location, selector, options, &block)
|
19
30
|
end
|
20
31
|
|
32
|
+
def negative_mock_subject_method(selector, options, &block)
|
33
|
+
location = options[:expected_from] || caller(1)[2]
|
34
|
+
subject_mock_proxy.add_negative_message_expectation(location, selector, &block)
|
35
|
+
end
|
36
|
+
|
21
37
|
def expectation_chain(*args)
|
22
38
|
if defined?(RSpec::Mocks::AnyInstance::PositiveExpectationChain)
|
23
39
|
RSpec::Mocks::AnyInstance::PositiveExpectationChain.new(*args)
|
@@ -26,5 +42,13 @@ module BetterReceive
|
|
26
42
|
end
|
27
43
|
end
|
28
44
|
|
45
|
+
def negative_expectation_chain(*args)
|
46
|
+
if defined?(RSpec::Mocks::AnyInstance::NegativeExpectationChain)
|
47
|
+
RSpec::Mocks::AnyInstance::NegativeExpectationChain.new(*args)
|
48
|
+
else
|
49
|
+
RSpec::Mocks::AnyInstance::NegativeExpectationChain.new(*args)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
29
53
|
end
|
30
54
|
end
|
data/lib/better_receive/stub.rb
CHANGED
@@ -60,6 +60,15 @@ describe BetterReceive::Mock do
|
|
60
60
|
|
61
61
|
br_mock.assert_with(:bar, passed: true, &block_param)
|
62
62
|
end
|
63
|
+
|
64
|
+
it "converts the selector to a symbol if necessary" do
|
65
|
+
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
|
66
|
+
mock_proxy.should_receive(:add_message_expectation) do |*args, &block|
|
67
|
+
args[1].should == :bar
|
68
|
+
end
|
69
|
+
|
70
|
+
br_mock.assert_with('bar', passed: true, &block_param)
|
71
|
+
end
|
63
72
|
end
|
64
73
|
|
65
74
|
context "on .any_instance" do
|
@@ -99,4 +108,106 @@ describe BetterReceive::Mock do
|
|
99
108
|
end
|
100
109
|
end
|
101
110
|
end
|
111
|
+
|
112
|
+
describe "#assert_negative_with" do
|
113
|
+
let(:foo) { Foo.new }
|
114
|
+
let(:br_mock) { BetterReceive::Mock.new(foo) }
|
115
|
+
|
116
|
+
it "raises an error if the method is not defined" do
|
117
|
+
expect {
|
118
|
+
br_mock.assert_negative_with :bar_baz
|
119
|
+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError) { |error|
|
120
|
+
error.message.should =~ /to respond to :bar_baz/
|
121
|
+
}
|
122
|
+
end
|
123
|
+
|
124
|
+
context "when mocking" do
|
125
|
+
let(:mock_proxy) { double(RSpec::Mocks::Proxy) }
|
126
|
+
|
127
|
+
it "creates a mock proxy and adds an expectation to it" do
|
128
|
+
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
|
129
|
+
mock_proxy.should_receive(:add_negative_message_expectation)
|
130
|
+
|
131
|
+
br_mock.assert_negative_with :bar
|
132
|
+
end
|
133
|
+
|
134
|
+
it "returns an rspec message expectation(responds to additional matchers ('with', 'once'...))" do
|
135
|
+
foo.better_not_receive(:bar).should be_a RSpec::Mocks::NegativeMessageExpectation
|
136
|
+
|
137
|
+
expect {
|
138
|
+
foo.bar
|
139
|
+
}.to raise_error(RSpec::Mocks::MockExpectationError) { |error|
|
140
|
+
error.message.should include "received: 1 time"
|
141
|
+
}
|
142
|
+
|
143
|
+
br_mock.assert_negative_with(:bar).with('wibble')
|
144
|
+
expect {
|
145
|
+
foo.bar('wibble')
|
146
|
+
}.to raise_error(RSpec::Mocks::MockExpectationError) { |error|
|
147
|
+
error.message.should include "received: 1 time"
|
148
|
+
}
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
context "and passing arguments" do
|
153
|
+
let(:block_param) { Proc.new {} }
|
154
|
+
let(:options) { {passed: true} }
|
155
|
+
|
156
|
+
it "passes all arguments through to the mock_proxy" do
|
157
|
+
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
|
158
|
+
mock_proxy.should_receive(:add_negative_message_expectation) do |*args, &block|
|
159
|
+
args[1].should == :bar
|
160
|
+
block.should == block_param
|
161
|
+
end
|
162
|
+
|
163
|
+
br_mock.assert_negative_with(:bar, &block_param)
|
164
|
+
end
|
165
|
+
|
166
|
+
it "converts the selector to a symbol" do
|
167
|
+
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
|
168
|
+
mock_proxy.should_receive(:add_negative_message_expectation) do |*args, &block|
|
169
|
+
args[1].should == :bar
|
170
|
+
end
|
171
|
+
|
172
|
+
br_mock.assert_negative_with("bar", &block_param)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
context "on .any_instance" do
|
177
|
+
let(:br_mock) { BetterReceive::Mock.new(Foo.any_instance) }
|
178
|
+
|
179
|
+
context "when the method is defined" do
|
180
|
+
context "and the method is called" do
|
181
|
+
it 'raises when called' do
|
182
|
+
expect {
|
183
|
+
br_mock.assert_negative_with(:bar)
|
184
|
+
foo.bar
|
185
|
+
}.to raise_error(RSpec::Mocks::MockExpectationError) { |error|
|
186
|
+
error.message.should include "received: 1 time"
|
187
|
+
}
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
context 'and the method is not called' do
|
192
|
+
it 'does not raise an error' do
|
193
|
+
expect {
|
194
|
+
br_mock.assert_negative_with(:bar)
|
195
|
+
RSpec::Mocks::space.verify_all
|
196
|
+
}.to_not raise_error
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
context 'when the method is not defined' do
|
202
|
+
it 'raises an error' do
|
203
|
+
expect {
|
204
|
+
br_mock.assert_negative_with(:baz)
|
205
|
+
}.to raise_error { |error|
|
206
|
+
error.message.should == "Expected instances of Foo to respond to :baz"
|
207
|
+
}
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
102
213
|
end
|
@@ -51,14 +51,23 @@ describe BetterReceive::Stub do
|
|
51
51
|
it "passes all arguments through to the mock_proxy" do
|
52
52
|
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
|
53
53
|
mock_proxy.should_receive(:add_stub) do |*args, &block|
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
args[1].should == :bar
|
55
|
+
args[2].should == options
|
56
|
+
block.should == block_param
|
57
57
|
end
|
58
58
|
|
59
59
|
br_stub.assert_with(:bar, passed: true, &block_param)
|
60
60
|
end
|
61
61
|
|
62
|
+
it "converts the selector to a string" do
|
63
|
+
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
|
64
|
+
mock_proxy.should_receive(:add_stub) do |*args, &block|
|
65
|
+
args[1].should == :bar
|
66
|
+
end
|
67
|
+
|
68
|
+
br_stub.assert_with("bar", passed: true, &block_param)
|
69
|
+
end
|
70
|
+
|
62
71
|
it "returns the value passed in the block" do
|
63
72
|
br_stub.assert_with(:bar) { :baz }
|
64
73
|
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_receive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.5.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Steve Ellis
|
@@ -10,12 +9,11 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date: 2013-
|
12
|
+
date: 2013-06-27 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: rspec
|
17
16
|
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
17
|
requirements:
|
20
18
|
- - ~>
|
21
19
|
- !ruby/object:Gem::Version
|
@@ -23,7 +21,6 @@ dependencies:
|
|
23
21
|
type: :runtime
|
24
22
|
prerelease: false
|
25
23
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
-
none: false
|
27
24
|
requirements:
|
28
25
|
- - ~>
|
29
26
|
- !ruby/object:Gem::Version
|
@@ -31,33 +28,29 @@ dependencies:
|
|
31
28
|
- !ruby/object:Gem::Dependency
|
32
29
|
name: rake
|
33
30
|
requirement: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
31
|
requirements:
|
36
|
-
- -
|
32
|
+
- - '>='
|
37
33
|
- !ruby/object:Gem::Version
|
38
34
|
version: '0'
|
39
35
|
type: :development
|
40
36
|
prerelease: false
|
41
37
|
version_requirements: !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
38
|
requirements:
|
44
|
-
- -
|
39
|
+
- - '>='
|
45
40
|
- !ruby/object:Gem::Version
|
46
41
|
version: '0'
|
47
42
|
- !ruby/object:Gem::Dependency
|
48
43
|
name: pry
|
49
44
|
requirement: !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
45
|
requirements:
|
52
|
-
- -
|
46
|
+
- - '>='
|
53
47
|
- !ruby/object:Gem::Version
|
54
48
|
version: '0'
|
55
49
|
type: :development
|
56
50
|
prerelease: false
|
57
51
|
version_requirements: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
52
|
requirements:
|
60
|
-
- -
|
53
|
+
- - '>='
|
61
54
|
- !ruby/object:Gem::Version
|
62
55
|
version: '0'
|
63
56
|
description: Assert that an object responds to a method before asserting that the
|
@@ -87,27 +80,26 @@ files:
|
|
87
80
|
- spec/spec_helper.rb
|
88
81
|
homepage: http://github.com/se3000/better_receive
|
89
82
|
licenses: []
|
83
|
+
metadata: {}
|
90
84
|
post_install_message:
|
91
85
|
rdoc_options: []
|
92
86
|
require_paths:
|
93
87
|
- lib
|
94
88
|
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
-
none: false
|
96
89
|
requirements:
|
97
|
-
- -
|
90
|
+
- - '>='
|
98
91
|
- !ruby/object:Gem::Version
|
99
92
|
version: '0'
|
100
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
-
none: false
|
102
94
|
requirements:
|
103
|
-
- -
|
95
|
+
- - '>='
|
104
96
|
- !ruby/object:Gem::Version
|
105
97
|
version: '0'
|
106
98
|
requirements: []
|
107
99
|
rubyforge_project:
|
108
|
-
rubygems_version:
|
100
|
+
rubygems_version: 2.0.0
|
109
101
|
signing_key:
|
110
|
-
specification_version:
|
102
|
+
specification_version: 4
|
111
103
|
summary: A more assertive mock.
|
112
104
|
test_files:
|
113
105
|
- spec/lib/better_receive/mock_spec.rb
|