better_receive 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/better_receive.gemspec +1 -0
- data/lib/better_receive.rb +4 -5
- data/lib/better_receive/mock.rb +29 -0
- data/lib/better_receive/version.rb +1 -1
- data/spec/lib/better_receive_spec.rb +25 -18
- data/spec/spec_helper.rb +1 -0
- metadata +19 -2
data/better_receive.gemspec
CHANGED
data/lib/better_receive.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
require "better_receive/version"
|
2
|
+
require "better_receive/mock"
|
2
3
|
|
3
4
|
module BetterReceive
|
4
|
-
def better_receive(method_name,
|
5
|
-
|
6
|
-
|
7
|
-
self.should_receive(method_name, *args, &block)
|
5
|
+
def better_receive(method_name, options={}, &block)
|
6
|
+
Mock.new(self).verify(method_name, options, &block)
|
8
7
|
end
|
9
8
|
end
|
10
9
|
|
11
|
-
BasicObject.class_eval
|
10
|
+
BasicObject.class_eval { include BetterReceive }
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module BetterReceive
|
2
|
+
class Mock
|
3
|
+
attr_reader :expectation, :object
|
4
|
+
|
5
|
+
def initialize(object)
|
6
|
+
@object = object
|
7
|
+
end
|
8
|
+
|
9
|
+
def verify(selector, options, &block)
|
10
|
+
object.should respond_to(selector)
|
11
|
+
|
12
|
+
mock_method(selector, options, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def mock_method(selector, options, &block)
|
18
|
+
mock_proxy = object.send(:__mock_proxy)
|
19
|
+
|
20
|
+
location = options[:expected_from] || caller(1)[2]
|
21
|
+
@expectation = mock_proxy.add_message_expectation(location, selector, options, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def respond_to(selector)
|
25
|
+
RSpec::Matchers::BuiltIn::RespondTo.new(selector)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -25,33 +25,40 @@ describe BetterReceive do
|
|
25
25
|
}
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
context "when mocking" do
|
29
|
+
let(:mock_proxy) { double(RSpec::Mocks::Proxy) }
|
30
30
|
|
31
|
-
|
31
|
+
it "creates a mock proxy and adds an expectation to it" do
|
32
|
+
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
|
33
|
+
mock_proxy.should_receive(:add_message_expectation)
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
+
foo.better_receive(:bar)
|
36
|
+
end
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
+
it "returns an rspec message expectation(responds to additional matchers ('with', 'once'...))" do
|
39
|
+
foo.better_receive(:bar).should be_a RSpec::Mocks::MessageExpectation
|
38
40
|
|
39
|
-
|
41
|
+
foo.bar
|
40
42
|
|
41
|
-
|
43
|
+
foo.better_receive(:bar).with('wibble')
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
+
foo.bar('wibble')
|
46
|
+
end
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
arg2 = 2
|
50
|
-
block = Proc.new {}
|
48
|
+
context "and passing arguments" do
|
49
|
+
let(:block_param) { Proc.new {} }
|
50
|
+
let(:options) { {passed: true} }
|
51
51
|
|
52
|
-
|
52
|
+
it "passes all arguments through to the mock_proxy" do
|
53
|
+
foo.should_receive(:send).with(:__mock_proxy).and_return(mock_proxy)
|
54
|
+
mock_proxy.should_receive(:add_message_expectation) do |*args, &block|
|
55
|
+
args[1].should == :bar
|
56
|
+
args[2].should == options
|
57
|
+
block.should == block_param
|
58
|
+
end
|
53
59
|
|
54
|
-
|
60
|
+
foo.better_receive(:bar, passed: true, &block_param)
|
61
|
+
end
|
55
62
|
end
|
56
63
|
end
|
57
64
|
end
|
data/spec/spec_helper.rb
CHANGED
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.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-01-
|
13
|
+
date: 2013-01-13 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
@@ -44,6 +44,22 @@ dependencies:
|
|
44
44
|
- - ! '>='
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: pry
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
47
63
|
description: Assert that an object responds to a method before asserting that the
|
48
64
|
method is received.
|
49
65
|
email:
|
@@ -61,6 +77,7 @@ files:
|
|
61
77
|
- Rakefile
|
62
78
|
- better_receive.gemspec
|
63
79
|
- lib/better_receive.rb
|
80
|
+
- lib/better_receive/mock.rb
|
64
81
|
- lib/better_receive/version.rb
|
65
82
|
- spec/lib/better_receive_spec.rb
|
66
83
|
- spec/spec_helper.rb
|