rspec_candy 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,26 +3,36 @@ module RSpecCandy
3
3
  module ShouldReceiveAndExecute
4
4
 
5
5
  def should_receive_and_execute(method)
6
- method_base = method.to_s.gsub(/([\?\!\=\[\]]+)$/, '')
6
+
7
+ method = method.to_s
8
+ method_base = method.gsub(/([\?\!\=\[\]]+)$/, '')
7
9
  method_suffix = $1
8
10
 
9
11
  method_called = "_#{method_base}_called#{method_suffix}"
10
12
  method_with_spy = "#{method_base}_with_spy#{method_suffix}"
11
13
  method_without_spy = "#{method_base}_without_spy#{method_suffix}"
12
14
 
15
+ method_defined_directly = (private_methods + protected_methods + public_methods).include?(method) # not method_missing
16
+
13
17
  prototype = respond_to?(:singleton_class) ? singleton_class : metaclass
14
18
  prototype.class_eval do
15
19
 
16
- unless method_defined?(method_with_spy)
20
+ define_method method_called do |*args|
21
+ end
17
22
 
18
- define_method method_called do |*args|
23
+ if method_defined_directly
24
+ unless method_defined?(method_with_spy)
25
+ define_method method_with_spy do |*args, &block|
26
+ send(method_called, *args)
27
+ send(method_without_spy, *args, &block)
28
+ end
29
+ alias_method_chain method, :spy
19
30
  end
20
-
21
- define_method method_with_spy do |*args, &block|
31
+ else
32
+ define_method method do |*args, &block|
22
33
  send(method_called, *args)
23
- send(method_without_spy, *args, &block)
34
+ super
24
35
  end
25
- alias_method_chain method, :spy
26
36
  end
27
37
 
28
38
  end
@@ -1,3 +1,3 @@
1
1
  module RSpecCandy
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
@@ -25,6 +25,29 @@ describe RSpecCandy::Helpers::ShouldReceiveAndExecute do
25
25
  object.size # make the example pass
26
26
  end
27
27
 
28
+ it "should not overwrite the implementation of a class method defined through an object's singleton class" do
29
+ object = Object.new
30
+ def object.foo
31
+ 'foo'
32
+ end
33
+ object.should_receive_and_execute(:foo)
34
+ object.foo.should == 'foo'
35
+ end
36
+
37
+ it 'should not fail for a class that responds to messages using #method_missing' do
38
+ klass = Class.new do
39
+ def respond_to?(*args)
40
+ true
41
+ end
42
+ def method_missing(symbol, *args, &block)
43
+ 'foo'
44
+ end
45
+ end
46
+ object = klass.new
47
+ object.should_receive_and_execute(:foo)
48
+ object.foo.should == 'foo'
49
+ end
50
+
28
51
  end
29
52
 
30
53
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec_candy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 3
10
- version: 0.2.3
9
+ - 4
10
+ version: 0.2.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Henning Koch