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.
@@ -17,4 +17,5 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  gem.add_dependency("rspec", "~> 2.0")
19
19
  gem.add_development_dependency("rake", ">= 0")
20
+ gem.add_development_dependency("pry", ">= 0")
20
21
  end
@@ -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, *args, &block)
5
- self.should RSpec::Matchers::BuiltIn::RespondTo.new(method_name)
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 { include BetterReceive }
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
@@ -1,3 +1,3 @@
1
1
  module BetterReceive
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -25,33 +25,40 @@ describe BetterReceive do
25
25
  }
26
26
  end
27
27
 
28
- it "checks that the object receives the specified method" do
29
- foo.should_receive(:should_receive).and_call_original
28
+ context "when mocking" do
29
+ let(:mock_proxy) { double(RSpec::Mocks::Proxy) }
30
30
 
31
- foo.better_receive(:bar)
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
- foo.bar
34
- end
35
+ foo.better_receive(:bar)
36
+ end
35
37
 
36
- it "returns an rspec mock object(responds to additional matchers ('with', 'once'...))" do
37
- foo.better_receive(:bar).should be_a RSpec::Mocks::MessageExpectation
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
- foo.bar
41
+ foo.bar
40
42
 
41
- foo.better_receive(:bar).with('wibble')
43
+ foo.better_receive(:bar).with('wibble')
42
44
 
43
- foo.bar('wibble')
44
- end
45
+ foo.bar('wibble')
46
+ end
45
47
 
46
- context "when passing arguments" do
47
- it "passes all arguments through to should_receive" do
48
- arg1 = 1
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
- foo.should_receive(:should_receive).with(:bar, arg1, arg2, block)
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
- foo.better_receive(:bar, arg1, arg2, block)
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
@@ -5,6 +5,7 @@
5
5
  #
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
7
  require 'better_receive'
8
+ require 'pry'
8
9
 
9
10
  RSpec.configure do |config|
10
11
  config.treat_symbols_as_metadata_keys_with_true_values = true
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.0
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-05 00:00:00.000000000 Z
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