better_receive 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2374b6397328e21d63f479e68e74a2dfdc843960
4
- data.tar.gz: c4e755a254de947b2fe8bed0bcca4e390177a513
3
+ metadata.gz: 7dba7386ed326d7d13c41a1b901255f356d9547c
4
+ data.tar.gz: 8d01d997bf38eadbb9945e3f79284d6689baf4d3
5
5
  SHA512:
6
- metadata.gz: 9c755925be275f776cb9e7bfc86372785b96fb96a348583b2d03252d4e4b4771f13ab7189652a6e498f7de6cbdc7937d87406c129a71b324fc4b8a00289412b8
7
- data.tar.gz: 2692f614cf4c2803c7f24e91fa9fc92ec545c5e54432aadc9a943934155ac54900bcb0c16ee9cb25c0c82121811ff00d853bd4d786c281288fd1e84abc211085
6
+ metadata.gz: 8d2fc544f91d3a9ae0af34325406dfb7951c97b26bb406ec232e7b79a104bdc106050ca38d0c1ec770921cfe2f1e33e6b071188231d9cb13511250ecf4d27482
7
+ data.tar.gz: aa613a45884efc818abcefcbf5d44cd05663bd141f0c3eeeac3bb90b1a920f15ac11906190039923f2a82bd9e06e036c2d2d5888c95dc203b2ec605a8ae1db04
data/README.md CHANGED
@@ -9,7 +9,6 @@ Test drive new functionality and prevent bugs by asserting objects respond to me
9
9
 
10
10
  ## Usage
11
11
 
12
-
13
12
  ```ruby
14
13
 
15
14
  class Foo; end
@@ -30,6 +29,8 @@ Any of these situation will raise an error because instances of Foo do not respo
30
29
  After the initial extra assertion, they continue to act like regular RSpec mocks/stubs.
31
30
 
32
31
 
32
+ If you are using a version of RSpec < 2.14, lock to BetterReceive version 0.5 or earlier.
33
+
33
34
  ## Contributing
34
35
 
35
36
  1. Fork it
@@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = BetterReceive::BETTER_VERSION
17
17
 
18
- gem.add_dependency("rspec", "~> 2.0")
18
+ gem.add_dependency("rspec", "~> 2.14")
19
19
  gem.add_development_dependency("rake", ">= 0")
20
20
  gem.add_development_dependency("pry", ">= 0")
21
21
  end
@@ -18,10 +18,6 @@ module BetterReceive
18
18
  RSpec::Matchers::BuiltIn::RespondTo.new(selector)
19
19
  end
20
20
 
21
- def subject_mock_proxy
22
- @mock_proxy ||= subject.send(:__mock_proxy)
23
- end
24
-
25
21
  def any_instance_better_expect(selector, options, &block)
26
22
  any_instance_should_respond_to selector
27
23
  any_instance_add_expectation selector, &block
@@ -40,21 +36,15 @@ module BetterReceive
40
36
  end
41
37
 
42
38
  def any_instance_add_expectation(selector, &block)
43
- RSpec::Mocks::space.add(subject)
44
-
45
- subject.instance_variable_set(:@expectation_set, true)
46
39
  subject.send(:observe!, selector)
47
40
 
48
41
  subject.message_chains.add(selector, expectation_chain(selector, &block))
49
42
  end
50
43
 
51
44
  def any_instance_add_negative_expectation(selector, &block)
52
- RSpec::Mocks::space.add(subject)
53
-
54
- subject.instance_variable_set(:@expectation_set, true)
55
45
  subject.send(:observe!, selector)
56
46
 
57
- subject.message_chains.add(selector, negative_expectation_chain(selector, &block))
47
+ subject.message_chains.add(selector, expectation_chain(selector, &block)).never
58
48
  end
59
49
 
60
50
  end
@@ -4,6 +4,7 @@ module BetterReceive
4
4
  def assert_with(selector, options={}, &block)
5
5
  selector = selector.to_sym
6
6
  if subject_is_any_instance?
7
+ subject.instance_variable_set(:@expectation_set, true)
7
8
  any_instance_better_expect(selector, options, &block)
8
9
  else
9
10
  subject.should respond_to selector
@@ -14,6 +15,7 @@ module BetterReceive
14
15
  def assert_negative_with(selector, options={}, &block)
15
16
  selector = selector.to_sym
16
17
  if subject_is_any_instance?
18
+ subject.instance_variable_set(:@expectation_set, true)
17
19
  any_instance_better_not_expect(selector, options, &block)
18
20
  else
19
21
  subject.should respond_to selector
@@ -24,6 +26,10 @@ module BetterReceive
24
26
 
25
27
  private
26
28
 
29
+ def subject_mock_proxy
30
+ @mock_proxy ||= RSpec::Mocks.proxy_for(subject)
31
+ end
32
+
27
33
  def mock_subject_method(selector, options, &block)
28
34
  location = options[:expected_from] || caller(1)[2]
29
35
  subject_mock_proxy.add_message_expectation(location, selector, options, &block)
@@ -31,24 +37,11 @@ module BetterReceive
31
37
 
32
38
  def negative_mock_subject_method(selector, options, &block)
33
39
  location = options[:expected_from] || caller(1)[2]
34
- subject_mock_proxy.add_negative_message_expectation(location, selector, &block)
40
+ subject_mock_proxy.add_message_expectation(location, selector, options, &block).never
35
41
  end
36
42
 
37
43
  def expectation_chain(*args)
38
- if defined?(RSpec::Mocks::AnyInstance::PositiveExpectationChain)
39
- RSpec::Mocks::AnyInstance::PositiveExpectationChain.new(*args)
40
- else
41
- RSpec::Mocks::AnyInstance::ExpectationChain.new(*args)
42
- end
44
+ RSpec::Mocks::AnyInstance::PositiveExpectationChain.new(*args)
43
45
  end
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
-
53
46
  end
54
47
  end
@@ -20,18 +20,12 @@ module BetterReceive
20
20
  any_instance_better_expect(selector, options, &block)
21
21
  else
22
22
  subject.should respond_to selector
23
- stub_subject_method(selector, options, &block)
23
+ subject.stub(selector, options, &block)
24
24
  end
25
25
  end
26
26
 
27
- def stub_subject_method(selector, options, &block)
28
- location = options[:expected_from] || caller(1)[2]
29
- subject_mock_proxy.add_stub(location, selector, options, &block)
30
- end
31
-
32
27
  def expectation_chain(*args)
33
28
  RSpec::Mocks::AnyInstance::StubChain.new(*args)
34
29
  end
35
-
36
30
  end
37
31
  end
@@ -1,3 +1,3 @@
1
1
  module BetterReceive
2
- BETTER_VERSION = "0.5.0"
2
+ BETTER_VERSION = "0.6.0"
3
3
  end
@@ -26,86 +26,82 @@ describe BetterReceive::Mock do
26
26
  }
27
27
  end
28
28
 
29
- context "when mocking" do
30
- let(:mock_proxy) { double(RSpec::Mocks::Proxy) }
29
+ it "returns an rspec message expectation" do
30
+ foo.better_receive(:bar).should be_a RSpec::Mocks::MessageExpectation
31
31
 
32
- it "creates a mock proxy and adds an expectation to it" do
33
- foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
34
- mock_proxy.should_receive(:add_message_expectation)
32
+ foo.bar
33
+ end
35
34
 
36
- br_mock.assert_with :bar
37
- end
35
+ it "responds to additional matchers(:with, :once...)" do
36
+ br_mock.assert_with(:bar).with('wibble')
38
37
 
39
- it "returns an rspec message expectation(responds to additional matchers ('with', 'once'...))" do
40
- foo.better_receive(:bar).should be_a RSpec::Mocks::MessageExpectation
38
+ foo.bar('wibble')
39
+ end
41
40
 
42
- foo.bar
41
+ context "passing arguments" do
42
+ let(:selector) { :bar }
43
+ let(:options) { {passed: true} }
44
+ let(:block_param) { Proc.new {} }
43
45
 
44
- br_mock.assert_with(:bar).with('wibble')
46
+ before do
47
+ br_mock.assert_with(:bar, options, &block_param)
45
48
 
46
- foo.bar('wibble')
49
+ @expectation = RSpec::Mocks.proxy_for(foo).send(:method_doubles)[0][:expectations][0]
47
50
  end
48
51
 
49
- context "and passing arguments" do
50
- let(:block_param) { Proc.new {} }
51
- let(:options) { {passed: true} }
52
-
53
- it "passes all arguments through to the mock_proxy" do
54
- foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
55
- mock_proxy.should_receive(:add_message_expectation) do |*args, &block|
56
- args[1].should == :bar
57
- args[2].should == options
58
- block.should == block_param
59
- end
52
+ after do
53
+ foo.bar
54
+ end
60
55
 
61
- br_mock.assert_with(:bar, passed: true, &block_param)
62
- end
56
+ it "passes all arguments through to the mock_proxy" do
57
+ @expectation.message.should == :bar
58
+ @expectation.implementation.inner_action.should == block_param
59
+ @expectation.send(:error_generator).opts.should == options
60
+ end
63
61
 
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
62
+ context "when the selector is a string" do
63
+ let(:selector) { "bar" }
69
64
 
70
- br_mock.assert_with('bar', passed: true, &block_param)
65
+ it "converts the selector to a symbol" do
66
+ @expectation.message.should == selector.to_sym
71
67
  end
72
68
  end
69
+ end
73
70
 
74
- context "on .any_instance" do
75
- let(:br_mock) { BetterReceive::Mock.new(Foo.any_instance) }
76
-
77
- context "when the method is defined" do
78
- context "and the method is called" do
79
- it 'does not raise an error' do
80
- expect {
81
- br_mock.assert_with(:bar)
82
- foo.bar
83
- }.to_not raise_error
84
- end
85
- end
71
+ context "on .any_instance" do
72
+ let(:br_mock) { BetterReceive::Mock.new(Foo.any_instance) }
86
73
 
87
- context 'and the method is not called' do
88
- it 'does raise an error' do
89
- expect {
90
- br_mock.assert_with(:bar)
91
- RSpec::Mocks::space.verify_all
92
- }.to raise_error(RSpec::Mocks::MockExpectationError) { |error|
93
- error.message.should == "Exactly one instance should have received the following message(s) but didn't: bar"
94
- }
95
- end
74
+ context "when the method is defined" do
75
+ context "and the method is called" do
76
+ it 'does not raise an error' do
77
+ expect {
78
+ br_mock.assert_with(:bar)
79
+ foo.bar
80
+ }.to_not raise_error
96
81
  end
97
82
  end
98
83
 
99
- context 'when the method is not defined' do
100
- it 'raises an error' do
84
+ context 'and the method is not called' do
85
+ it 'does raise an error' do
101
86
  expect {
102
- br_mock.assert_with(:baz)
103
- }.to raise_error { |error|
104
- error.message.should == "Expected instances of Foo to respond to :baz"
87
+ br_mock.assert_with(:bar)
88
+ RSpec::Mocks::space.verify_all
89
+ }.to raise_error(RSpec::Mocks::MockExpectationError) { |error|
90
+ error.message.should == "Exactly one instance should have received the following message(s) but didn't: bar"
105
91
  }
106
92
  end
107
93
  end
108
94
  end
95
+
96
+ context 'when the method is not defined' do
97
+ it 'raises an error' do
98
+ expect {
99
+ br_mock.assert_with(:baz)
100
+ }.to raise_error { |error|
101
+ error.message.should == "Expected instances of Foo to respond to :baz"
102
+ }
103
+ end
104
+ end
109
105
  end
110
106
  end
111
107
 
@@ -121,91 +117,77 @@ describe BetterReceive::Mock do
121
117
  }
122
118
  end
123
119
 
124
- context "when mocking" do
125
- let(:mock_proxy) { double(RSpec::Mocks::Proxy) }
120
+ it "returns an rspec message expectation" do
121
+ br_mock.assert_negative_with(:bar).should be_a RSpec::Mocks::MessageExpectation
126
122
 
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)
123
+ expect {
124
+ foo.bar
125
+ }.to raise_error(RSpec::Mocks::MockExpectationError) { |error|
126
+ error.message.should include "received: 1 time"
127
+ }
128
+ end
130
129
 
131
- br_mock.assert_negative_with :bar
132
- end
130
+ it "responds to additional matchers(:with, :once...)" do
131
+ br_mock.assert_negative_with(:bar).with('wibble')
132
+ expect {
133
+ foo.bar('wibble')
134
+ }.to raise_error(RSpec::Mocks::MockExpectationError) { |error|
135
+ error.message.should include "received: 1 time"
136
+ }
137
+ end
133
138
 
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
139
+ context "passing arguments" do
140
+ let(:selector) { :bar }
141
+ let(:options) { {passed: true} }
142
+ let(:block_param) { Proc.new {} }
136
143
 
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
- }
144
+ before do
145
+ br_mock.assert_negative_with(:bar, options, &block_param)
149
146
 
147
+ @expectation = RSpec::Mocks.proxy_for(foo).send(:method_doubles)[0][:expectations][0]
150
148
  end
151
149
 
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
150
+ it "passes all arguments through to the mock_proxy" do
151
+ @expectation.message.should == :bar
152
+ @expectation.implementation.inner_action.should == block_param
153
+ @expectation.send(:error_generator).opts.should == options
154
+ end
162
155
 
163
- br_mock.assert_negative_with(:bar, &block_param)
164
- end
156
+ context "when the selector is a string" do
157
+ let(:selector) { "bar" }
165
158
 
166
159
  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)
160
+ @expectation.message.should == selector.to_sym
173
161
  end
174
162
  end
163
+ end
175
164
 
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
165
+ context "on .any_instance" do
166
+ let(:br_mock) { BetterReceive::Mock.new(Foo.any_instance) }
190
167
 
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
168
+ it 'raises when called' do
169
+ expect {
170
+ br_mock.assert_negative_with(:bar)
171
+ foo.bar
172
+ }.to raise_error(RSpec::Mocks::MockExpectationError) { |error|
173
+ error.message.should include "received: 1 time"
174
+ }
175
+ end
200
176
 
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
177
+ it 'does not raise an error if not called' do
178
+ expect {
179
+ br_mock.assert_negative_with(:bar)
180
+ RSpec::Mocks::space.verify_all
181
+ }.to_not raise_error
182
+ end
183
+
184
+ context 'when the method is not defined' do
185
+ it 'raises an error' do
186
+ expect {
187
+ br_mock.assert_negative_with(:baz)
188
+ }.to raise_error { |error|
189
+ error.message.should == "Expected instances of Foo to respond to :baz"
190
+ }
209
191
  end
210
192
  end
211
193
  end
@@ -11,67 +11,57 @@ describe BetterReceive::Stub do
11
11
  let(:br_stub) { BetterReceive::Stub.new(foo) }
12
12
 
13
13
  context "when passed a single selector" do
14
- context "when checking responds to" do
15
- it "determines whether an object responds to a method" do
16
- foo.should_receive(:respond_to?).with(:bar).and_return(true)
14
+ it "determines whether an object responds to a method" do
15
+ foo.should_receive(:respond_to?).with(:bar).and_return(true)
17
16
 
18
- br_stub.assert_with :bar
19
-
20
- foo.bar
21
- end
17
+ br_stub.assert_with :bar
22
18
 
23
- it "raises an error if the method is not defined" do
24
- expect {
25
- br_stub.assert_with :bar_baz
26
- }.to raise_error(RSpec::Expectations::ExpectationNotMetError) { |error|
27
- error.message.should =~ /to respond to :bar_baz/
28
- }
29
- end
19
+ foo.bar
30
20
  end
31
21
 
32
- context "when stubbing" do
33
- let(:mock_proxy) { double(RSpec::Mocks::Proxy).as_null_object }
34
- let(:block_param) { Proc.new {} }
35
- let(:options) { {passed: true} }
22
+ it "raises an error if the method is not defined" do
23
+ expect {
24
+ br_stub.assert_with :bar_baz
25
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError) { |error|
26
+ error.message.should =~ /to respond to :bar_baz/
27
+ }
28
+ end
36
29
 
37
- it "creates a mock proxy and adds an expectation to it" do
38
- foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
39
- mock_proxy.should_receive(:add_stub)
30
+ it "returns an rspec message expectation(responds to additional matchers ('with', 'once'...))" do
31
+ br_stub.assert_with(:bar).should be_a RSpec::Mocks::MessageExpectation
40
32
 
41
- br_stub.assert_with :bar
42
- end
33
+ br_stub.assert_with(:bar).with('wibble')
34
+ end
43
35
 
44
- it "returns an rspec message expectation(responds to additional matchers ('with', 'once'...))" do
45
- br_stub.assert_with(:bar).should be_a RSpec::Mocks::MessageExpectation
36
+ it "returns the value passed in the block" do
37
+ br_stub.assert_with(:bar) { :baz }
46
38
 
39
+ foo.bar.should == :baz
40
+ end
47
41
 
48
- br_stub.assert_with(:bar).with('wibble')
49
- end
42
+ context "when passing arguments" do
43
+ let(:selector) { :bar }
44
+ let(:options) { {passed: true} }
45
+ let(:block_param) { Proc.new {} }
50
46
 
51
- it "passes all arguments through to the mock_proxy" do
52
- foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
53
- mock_proxy.should_receive(:add_stub) do |*args, &block|
54
- args[1].should == :bar
55
- args[2].should == options
56
- block.should == block_param
57
- end
47
+ before do
48
+ br_stub.assert_with(:bar, options, &block_param)
58
49
 
59
- br_stub.assert_with(:bar, passed: true, &block_param)
50
+ @stub = RSpec::Mocks.proxy_for(foo).send(:method_doubles)[0][:stubs][0]
60
51
  end
61
52
 
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)
53
+ it "creates a mock proxy and adds an expectation to it" do
54
+ @stub.message.should == :bar
55
+ @stub.implementation.inner_action.should == block_param
56
+ @stub.send(:error_generator).opts.should == options
69
57
  end
70
58
 
71
- it "returns the value passed in the block" do
72
- br_stub.assert_with(:bar) { :baz }
59
+ context "when the selector is a string" do
60
+ let(:selector) { "bar" }
73
61
 
74
- foo.bar.should == :baz
62
+ it "converts the selector to a symbol" do
63
+ @stub.message.should == selector.to_sym
64
+ end
75
65
  end
76
66
  end
77
67
  end
@@ -122,6 +112,10 @@ describe BetterReceive::Stub do
122
112
 
123
113
  foo.bar.should == :whatever
124
114
  end
115
+
116
+ it "does not blow up if the method is not called" do
117
+ br_stub.assert_with(:bar)
118
+ end
125
119
  end
126
120
 
127
121
  context 'when the method is not defined' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_receive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Ellis
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-27 00:00:00.000000000 Z
12
+ date: 2013-07-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - ~>
19
19
  - !ruby/object:Gem::Version
20
- version: '2.0'
20
+ version: '2.14'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - ~>
26
26
  - !ruby/object:Gem::Version
27
- version: '2.0'
27
+ version: '2.14'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement