mock any instance

Discussion in 'Python' started by Phlip, Dec 14, 2009.

  1. Phlip

    Phlip Guest

    Pythonistas:

    One important design principle is "construction encapsulation". That's
    where nobody creates anything, they always use things passed to them.

    Without this principle, when I need to mock the encapsulated item,
    some mock libraries provide an "any instance" facility.

    For example, here's a mock on one method of only one object:

    object = Thang()
    object.method = Mock()
    object.method() # <-- calls the mock, not the real method

    I need to mock the method on any instance spawned by a given class:

    Thang.method = MockAnyInstance()
    object = Thang()
    object.method() # <-- calls the mock, not the real method

    That will avoid "cutting a hole in the bulkhead" just to get to the
    'object = Thang()' line, to let me spoof out the Thang.

    Does anyone have any idea how to do that with the good-old Mocker
    here?

    http://python-mock.sourceforge.net/

    if I should use a different mocker, I would prefer it behave like that
    mock, for aesthetic reasons, and also to avoid the need to replace all
    our existing Mocks with a new one, following the rule that we should
    not use too many classes to do the same thing (DRY).

    --
    Phlip
    http://c2.com/cgi/wiki?ZeekLand
     
    Phlip, Dec 14, 2009
    #1
    1. Advertising

  2. Phlip

    Phlip Guest

    >    Thang.method = MockAnyInstance()
    >    object = Thang()
    >    object.method()  #  <--  calls the mock, not the real method


    The mocker here...

    http://www.voidspace.org.uk/python/mock/

    ....has @patch and @patch_object directives that might do it.

    (Luv it when you skate off the end of a language's original
    definition, huh?)

    > --
    >   Phlip
    >  http://c2.com/cgi/wiki?ZeekLand
     
    Phlip, Dec 14, 2009
    #2
    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. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Mock objects for nunit tests?

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Mar 17, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    5,019
  2. Ken Connor

    Mock Objects for Web Applications

    Ken Connor, Sep 15, 2003, in forum: Java
    Replies:
    0
    Views:
    331
    Ken Connor
    Sep 15, 2003
  3. Ken
    Replies:
    0
    Views:
    386
  4. fc2

    Where's Java mock exam?

    fc2, Nov 1, 2005, in forum: Java
    Replies:
    0
    Views:
    362
  5. Steve
    Replies:
    1
    Views:
    1,460
    Andrew Thompson
    Jan 4, 2006
Loading...

Share This Page