Created
June 15, 2010 12:20
-
-
Save mattdenner/439040 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyBase | |
def do_something | |
end | |
end | |
class MyDerived < MyBase | |
def do_something | |
do_something_else | |
super | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyBase | |
# When this method gets called it'll call itself, but itself in the MyDerived | |
# instance. | |
def do_something_with_testing | |
self.do_something_with_testing | |
end | |
alias_method_chain(:do_something, :testing) | |
end | |
class MyDerived | |
# This needs defining to prevent do_something_with_testing in MyBase recursing | |
# up it's own backside. | |
def do_something_with_testing | |
raise StandardError, 'You should be expecting a call here, use should_receive(:do_something_with_testing)' | |
end | |
end | |
describe MyDerived do | |
before(:each) do | |
@object = MyDerived.new | |
end | |
describe '#do_something' do | |
should 'call the super class implementation' do | |
@object.should_receive(:do_something_else).once.ordered | |
@object.should_receive(:do_something_with_testing).once.ordered | |
@object.do_something | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyDerived | |
def do_something_with_testing | |
raise StandardError, 'You should be expecting a call here, use should_receive(:do_something_with_testing)' | |
end | |
end | |
describe MyDerived do | |
before(:each) do | |
@object = MyDerived.new | |
end | |
context 'testing super' do | |
before(:each) do | |
MyBase.instance_eval do | |
define_method(:do_something_with_testing) { self.do_something_with_testing } | |
alias_method_chain(:do_something, :testing) | |
end | |
end | |
after(:each) do | |
MyBase.instance_eval do | |
alias_method(:do_something, :do_something_without_testing) | |
undef_method(:do_something_with_testing) | |
undef_method(:do_something_without_testing) | |
end | |
end | |
describe '#do_something' do | |
should 'call the super class implementation' do | |
@object.should_receive(:do_something_else).once.ordered | |
@object.should_receive(:do_something_with_testing).once.ordered | |
@object.do_something | |
end | |
end | |
end | |
describe '#some_other_method' do | |
# These tests won't have the super behaviour | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment