[rspec-users] rspec testing inheritance

David Chelimsky dchelimsky at gmail.com
Wed Sep 7 09:06:55 EDT 2011

On Sep 7, 2011, at 7:03 AM, Matt Wynne wrote:

> On 7 Sep 2011, at 07:53, Sidu Ponnappa wrote:
>> On 7 September 2011 11:09, Justin Ko <jko170 at gmail.com> wrote:
>>> On Tue, Sep 6, 2011 at 9:40 PM, slavix <mikerin.slava at gmail.com> wrote:
>>>> Hello,
>>>> Is there any way to test model inheritance in spec?
>>>> something like..
>>>> it { ChildModel.should < ParentModel }
>>>> thanks.
>> Wouldn't this be an implementation bound spec?
> +1
> Test the behaviour, not the implementation. You could look at using a shared example group if you want to specify that the subclass shared behaviour with the superclass.


It might not surprise you that I prefer to focus on behavior over implementation (though it might surprise some people who think I like to mock too much! ;)), however ...


... there are absolutely valid cases for focusing on type. In rspec-rails, for example, mock_models need to lie about their type to Rails' internals in order for things to run smoothly. There are, therefore, examples like:

  mock_model("User").should be_a(User)

Any sort of factory that might generate objects of different types in different contexts would warrant this as well.

That said, I'll guess that @slavix's motivation here is that there are ParentModel specs and he doesn't want to duplicate them for ChildModel. If that's true, then a shared group is definitely a better option for a number of reasons. The most obvious one is that ChildModel is free to override behavior defined in ParentModel, so the fact that ChildModel < ParentModel is no guarantee that they behave the same way.


More information about the rspec-users mailing list