rspec_super 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ RSpec Super
2
+ ===========
3
+ **Git**: [http://github.com/mattdenner/rspec_super](http://github.com/mattdenner/rspec_super)
4
+ **Author**: Matthew Denner
5
+ **Copyright**: 2010
6
+ **License**: MIT Licence
7
+
8
+ Overview
9
+ --------
10
+ This gem provides [RSpec](http://rspec.info/) with a way for testing methods that call super. Consider this:
11
+
12
+ class Base
13
+ def do_something
14
+ end
15
+ end
16
+
17
+ class Derived < Base
18
+ def do_something
19
+ super
20
+ do_something_else
21
+ end
22
+
23
+ def do_something_else
24
+ end
25
+ end
26
+
27
+ How do you go about testing `Derived#do_something` without having to duplicate your `Base#do_something` tests?
28
+
29
+ With this gem you can use {Spec::Mocks::Methods#should_receive_super_of should_receive_super_of} and the standard RSpec expectations, just like this:
30
+
31
+ describe Derived do
32
+ before(:each) do
33
+ @object = described_class.new
34
+ end
35
+
36
+ describe '#do_something' do
37
+ it 'calls super' do
38
+ @object.should_receive_super_of(:do_something).once
39
+ @object.should_receive(:do_something_else).once
40
+ @object.do_something
41
+ end
42
+ end
43
+ end
44
+
45
+ Word of warning!
46
+ ----------------
47
+ This is very very alpha code and has only been tested with RSpec 1.3.0 and on Ruby 1.9.1. You should be alright to use it on Ruby 1.8.7 but don't quote me on the RSpec support for earlier versions!
48
+
49
+ Copyright
50
+ ---------
51
+ RSpec Super &copy; 2010 by [Matthew Denner](mailto:matt.denner@gmail.com). RSpec Super is licensed under the MIT license.
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'yard'
5
+
6
+ YARD::Rake::YardocTask.new do |rd|
7
+ rd.files = [ 'README.markdown', 'lib/**/*.rb' ]
8
+ rd.options = [ '-o', 'doc' ]
9
+ end
10
+
11
+ require 'spec/rake/spectask'
12
+
13
+ Spec::Rake::SpecTask.new do |t|
14
+ t.spec_files = FileList[ 'spec/**/*_spec.rb' ]
15
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec/mocks/methods'
2
+
3
+ module Spec
4
+ module Mocks
5
+ module Methods
6
+ # Behaves like +should_receive+ except that it can be used to set expectations on the
7
+ # method specified calling super.
8
+ def should_receive_super_of(method)
9
+ should_receive(install_super_hook_for(method))
10
+ end
11
+
12
+ private
13
+
14
+ def rspec_reset_with_super
15
+ rspec_reset_without_super
16
+ uninstall_super_hooks
17
+ end
18
+ alias_method(:rspec_reset_without_super, :rspec_reset)
19
+ alias_method(:rspec_reset, :rspec_reset_with_super)
20
+
21
+ #--
22
+ # Complete fudge to make sure that YARD doesn't display rspec_reset_with_super in the
23
+ # documentation!
24
+ #++
25
+ public :rspec_reset
26
+
27
+ def uninstall_super_hooks #:nodoc:
28
+ @_super_hooks_installed ||= []
29
+ @_super_hooks_installed.each do |method|
30
+ method_with_super, method_without_super = :"#{ method }_with_super", :"#{ method }_without_super"
31
+
32
+ self.class.superclass.instance_eval do
33
+ alias_method(method, method_without_super)
34
+ undef_method(method_without_super)
35
+ undef_method(method_with_super)
36
+ end
37
+ end
38
+ end
39
+
40
+ def install_super_hook_for(method) #:nodoc:
41
+ method_with_super, method_without_super = :"#{ method }_with_super", :"#{ method }_without_super"
42
+
43
+ self.class.superclass.class_eval do
44
+ line_number = __LINE__ + 1
45
+ eval(%Q{
46
+ def #{ method_with_super }(*args, &block)
47
+ #{ method_with_super }(*args, &block)
48
+ end
49
+ alias_method(#{ method_without_super.inspect }, #{ method.inspect })
50
+ alias_method(#{ method.inspect }, #{ method_with_super.inspect })
51
+ }, binding, __FILE__, line_number)
52
+ end
53
+
54
+ @_super_hooks_installed ||= []
55
+ @_super_hooks_installed.push(method)
56
+
57
+ return method_with_super
58
+ end
59
+ end
60
+ end
61
+ end
62
+
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ class DirectlyDerived < Base
4
+ def do_something
5
+ super
6
+ do_something_else
7
+ end
8
+
9
+ def do_something_else
10
+
11
+ end
12
+ end
13
+
14
+ describe DirectlyDerived do
15
+ before(:each) do
16
+ @object = described_class.new
17
+ end
18
+
19
+ describe '#do_something' do
20
+ it 'raises without the super hook' do
21
+ lambda { @object.do_something }.should raise_error(StandardError, "Nope, that code didn't work")
22
+ end
23
+
24
+ it 'calls super' do
25
+ @object.should_receive_super_of(:do_something).once
26
+ @object.should_receive(:do_something_else).once
27
+ @object.do_something
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ class DerivedThroughIntermediary < Intermediary
4
+ def do_something
5
+ super
6
+ do_something_else
7
+ end
8
+
9
+ def do_something_else
10
+ end
11
+ end
12
+
13
+ describe DerivedThroughIntermediary do
14
+ before(:each) do
15
+ @object = described_class.new
16
+ end
17
+
18
+ describe '#do_something' do
19
+ it 'calls super' do
20
+ @object.should_receive_super_of(:do_something).once
21
+ @object.should_receive(:do_something_else).once
22
+ @object.do_something
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ class PassingArguments < Base
4
+ def do_something_with_arguments(a, b)
5
+ super
6
+ do_something_else_with_arguments(a, b)
7
+ end
8
+
9
+ def do_something_else_with_arguments(a, b)
10
+ end
11
+ end
12
+
13
+ describe PassingArguments do
14
+ before(:each) do
15
+ @object = described_class.new
16
+ end
17
+
18
+ describe '#do_something_with_arguments' do
19
+ it 'calls super with arguments' do
20
+ @object.should_receive_super_of(:do_something_with_arguments).with(1, 2).once
21
+ @object.should_receive(:do_something_else_with_arguments).with(1, 2).once
22
+ @object.do_something_with_arguments(1, 2)
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ class YieldingToBlock < Base
4
+ def do_something_with_block(&block)
5
+ super
6
+ do_something_else_with_block(&block)
7
+ end
8
+
9
+ def do_something_else_with_block(&block)
10
+ block.call
11
+ end
12
+ end
13
+
14
+ describe YieldingToBlock do
15
+ before(:each) do
16
+ @object = described_class.new
17
+ end
18
+
19
+ describe '#do_something_with_block' do
20
+ it 'causes two callbacks by default' do
21
+ callback = mock('callback')
22
+ callback.should_receive(:called).twice
23
+
24
+ @object.do_something_with_block { callback.called }
25
+ end
26
+
27
+ it 'calls super with block' do
28
+ callback = mock('callback')
29
+ callback.should_receive(:called).twice
30
+
31
+ @object.should_receive_super_of(:do_something_with_block).once.and_yield
32
+ @object.should_receive(:do_something_else_with_block).once.and_yield
33
+ @object.do_something_with_block { callback.called }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ require 'rspec_super'
2
+
3
+ class Base
4
+ def do_something
5
+ raise StandardError, "Nope, that code didn't work"
6
+ end
7
+
8
+ def do_something_with_arguments(a, b)
9
+ raise StandardError, "Nope, that code didn't work"
10
+ end
11
+
12
+ def do_something_with_block(&block)
13
+ yield
14
+ end
15
+ end
16
+
17
+ class Intermediary < Base
18
+
19
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec_super
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Matthew Denner
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-19 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 3
30
+ - 0
31
+ version: 1.3.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: yard
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 5
44
+ - 4
45
+ version: 0.5.4
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 3
58
+ - 0
59
+ version: 1.3.0
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ description: Provides helper methods for RSpec that help you test methods that call super
63
+ email: matt.denner@gmail.com
64
+ executables: []
65
+
66
+ extensions: []
67
+
68
+ extra_rdoc_files:
69
+ - README.markdown
70
+ files:
71
+ - README.markdown
72
+ - Rakefile
73
+ - lib/rspec_super.rb
74
+ - spec/rspec_super/direct_derivative_spec.rb
75
+ - spec/rspec_super/non_direct_derivative_spec.rb
76
+ - spec/rspec_super/passing_arguments_spec.rb
77
+ - spec/rspec_super/yielding_to_block_spec.rb
78
+ - spec/spec_helper.rb
79
+ has_rdoc: yard
80
+ homepage: http://github.com/mattdenner/rspec_super
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options:
85
+ - --charset=UTF-8
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ segments:
100
+ - 1
101
+ - 3
102
+ - 6
103
+ version: 1.3.6
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.3.6
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Test methods that call super with RSpec
111
+ test_files:
112
+ - spec/spec_helper.rb
113
+ - spec/rspec_super/direct_derivative_spec.rb
114
+ - spec/rspec_super/non_direct_derivative_spec.rb
115
+ - spec/rspec_super/passing_arguments_spec.rb
116
+ - spec/rspec_super/yielding_to_block_spec.rb