test that a superclass method gets called from a subclass in ruby with
Test::Unit and shoulda
So, I've got a superclass (We'll call SuperClass) and two sub-classes from
it (say SubClassA and SubClassB)
Now SuperClass has a method called my_method which SubClassA has an
override for. But SubClassB does not.
I've tested that the version on the superclass works, and that the
overrideden version on SubClassA works... but I'd also like to add a unit
test that checks that SubClassB calls the version on the superclass.
I could just copy/paste the test from the superclass... or I could write a
test-module that's called from both... but if it's working tin the
superclass - all I really need to do is check that the superclass
my_method gets successfully called when I call it on the subclas...
but how do I test that?
This is what I tried:
should "call superclass version of my_method from the sub_class" do
@sub_class = SubClassB.new({})
rval = :whatever
SuperClass.any_instance.expects(:my_method).returns(rval)
assert_equal rval, @sub_class.my_method(:anything)
end
what that gives me is this error:
unexpected invocation: #<AnyInstance:SubClassA>.nat_record(:anything)
unsatisfied expectations:
- expected exactly once, not yet invoked:
#<AnyInstance:SuperClass>.my_method(any_parameters)
test: call superclass version of my_method from the sub_class.
(SubClassATest)
It looks like it has decided that the my_method has been called on an
instance of SubClassA (which it has) and that it therefore has not been
called on the SuperClass instance.
Is there a better way to check it calls the superclass method?
No comments:
Post a Comment