Wow, that works great! Thanks all!
On Mar 28, 12:02 am, Ben Finney <[EMAIL PROTECTED]>
wrote:
> "Silfheed" <[EMAIL PROTECTED]> writes:
> ===== foo.py =====
> class Bar(object):
> def __init__(self):
> self.name = "bar"
> =====
>
> ===== dostuff.py =====
> import foo
>
> def get_bar():
> return foo.Bar()
> =====
>
> ===== test_dostuff.py =====
> import dostuff
>
> class Mock_foo_module(object):
> """ Mock object for foo module """
>
> class Bar(object):
> def __init__(self):
> self.name = "banana"
>
> def test_dostuff_get_bar_should_create_bar():
> """ The dostuff.get_bar function should create a new Bar """
> dostuff.foo = Mock_foo_module()
> test_bar = dostuff.get_bar()
> if not test_bar.name == "banana":
> raise AssertionError("test_bar.name should be banana")
> =====
>
> This is simply using an instance of a class (Mock_foo_module) to be a
> mock 'module' object. That mock module has the required 'Bar'
> attribute, bound to a class; so the 'dostuff' module's 'foo' attribute
> can be replaced with our mock module object for the purpose of the
> test.
>
> The mock module's Bar class behaves in an idiomatic way (setting the
> 'name' attribute of its instance to a known value) that we use to
> check whether the 'dostuff' module actually used our mock module.
>
> This can be extended to mock any module interface you like, and then
> confirm that the module under test has actually used the module as
> expected.
>
> --
> \ "A politician is an animal which can sit on a fence and yet |
> `\ keep both ears to the ground." -- Henry L. Mencken |
> _o__) |
> Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list