I just wrote another mock object framework.

Here it is:

class BazMock(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)

Now I need to figure out how to make some mock tests to go with it.

7 thoughts on “I just wrote another mock object framework.

  1. Doesn't seem too useful; when the code under test attempts to call expected methods on an instance it'll fail with 'AttributeError'. A mock object should pretend it's the object being mocked, surely?

  2. Hi Ben,

    I probably should have included a few example uses. Pretend I want to mock an object that has an attribute named ivr_sends which is a list of strings. I'd do this:

    mm = BazMock(ivr_sends=['abc', 'def'])

    Now my test code can access mm.ivr_sends just fine.

    And now pretend you need to mock attributes on attributes:

    mm = BazMock(x=BazMock(y=[BazMock(a=1, b=2), BazMock(a=3, b=4)]))

    And now I can access mm.x.y[0].a.

    For methods, you'd need to graft on definitions like this:

    def fake_foo(x):
    return “99”

    mm.foo = fake_foo

    I've been told that there are better mock objects out there, that do stuff like inspect a module or a class and create mocks automatically.

    I'm dragging my feet about learning them. Let me know if you find one you like.

Comments are closed.