Redefining inheritance?

Discussion in 'Ruby' started by Caleb Tennis, May 6, 2005.

  1. Caleb Tennis

    Caleb Tennis Guest

    Suppose I have this:

    class A
    class B < A

    Now, in my unit tests, I create a mock object to use for testing purposes:

    class MockA < A

    What I need now is a MockB, which inherits from MockA

    class MockB < MockA

    But I still need all of the methods which I have previous defined inside
    of B. Note that MockB doesn't have any relation to the actual B, unless I
    go back and redefine all of these methods within MockB itself.

    So, is there a way I can create a "MockB < B", but dynamically make "B
    inherit from MockA instead of the real A"
     
    Caleb Tennis, May 6, 2005
    #1
    1. Advertising

  2. Caleb Tennis wrote:
    > Suppose I have this:
    >
    > class A
    > class B < A
    >
    > Now, in my unit tests, I create a mock object to use for testing purposes:
    >
    > class MockA < A
    >
    > What I need now is a MockB, which inherits from MockA
    >
    > class MockB < MockA
    >
    > But I still need all of the methods which I have previous defined inside
    > of B. Note that MockB doesn't have any relation to the actual B, unless I
    > go back and redefine all of these methods within MockB itself.
    >
    > So, is there a way I can create a "MockB < B", but dynamically make "B
    > inherit from MockA instead of the real A"


    Do your mock classes define their own tests? If not, I'm not sure why
    you'd need MockB to extend MockA, and certainly not B to extend MockA.
    Make MockB extend B, which in turn extends A. If you really /must/ have
    MockB extend MockA, you could wrap an instance of the latter inside the
    former.
     
    jason r tibbetts, May 6, 2005
    #2
    1. Advertising

  3. "jason r tibbetts" <> schrieb im Newsbeitrag
    news:...
    > Caleb Tennis wrote:
    >> Suppose I have this:
    >>
    >> class A
    >> class B < A
    >>
    >> Now, in my unit tests, I create a mock object to use for testing
    >> purposes:
    >>
    >> class MockA < A
    >>
    >> What I need now is a MockB, which inherits from MockA
    >>
    >> class MockB < MockA
    >>
    >> But I still need all of the methods which I have previous defined inside
    >> of B. Note that MockB doesn't have any relation to the actual B, unless
    >> I
    >> go back and redefine all of these methods within MockB itself.
    >>
    >> So, is there a way I can create a "MockB < B", but dynamically make "B
    >> inherit from MockA instead of the real A"

    >
    > Do your mock classes define their own tests? If not, I'm not sure why
    > you'd need MockB to extend MockA, and certainly not B to extend MockA.
    > Make MockB extend B, which in turn extends A. If you really /must/ have
    > MockB extend MockA, you could wrap an instance of the latter inside the
    > former.


    .... or use modules instead. Hint: you can create instances with #alloc
    avoiding standard initialization.

    module MockA
    end

    module MockB
    include MockA
    end

    tester = B.alloc
    tester.extend MockB
    ....

    Kind regards

    robert
     
    Robert Klemme, May 6, 2005
    #3
  4. Caleb Tennis

    Caleb Tennis Guest


    > Do your mock classes define their own tests? If not, I'm not sure why
    > you'd need MockB to extend MockA, and certainly not B to extend MockA.
    > Make MockB extend B, which in turn extends A. If you really /must/ have
    > MockB extend MockA, you could wrap an instance of the latter inside the
    > former.


    A defines some methods which need to be overwritten during tests, which is why
    I have a MockA. B needs the same set of methods overwritten during its unit
    tests, so I end up needing a MockB which needs to grab the same overrides
    that were in A.

    I ended up just redefining the methods directly in A (thus, now no MockA is
    ever created) within the unit test file and did some aliasing to make it
    work. Thanks for the reply, though.
     
    Caleb Tennis, May 6, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Bassiliades

    Redefining an enumerated attribute type

    Nick Bassiliades, Dec 9, 2005, in forum: XML
    Replies:
    1
    Views:
    448
    Henry S. Thompson
    Dec 12, 2005
  2. uli
    Replies:
    5
    Views:
    4,904
    Dietmar Kuehl
    Apr 21, 2004
  3. Xiangliang Meng
    Replies:
    1
    Views:
    1,650
    Victor Bazarov
    Jun 21, 2004
  4. bberu
    Replies:
    7
    Views:
    668
    Ivan Vecerina
    Dec 18, 2004
  5. bberu
    Replies:
    1
    Views:
    730
    Ivan Vecerina
    Dec 17, 2004
Loading...

Share This Page