a pastebin project

Anonymous

  1. Mock object implementation.
  2.  
  3. This mock object implementation is meant to be very forgiving - it returns a
  4. new child Mock Object for every attribute accessed, and a mock object is
  5. returned from every method call.
  6.  
  7. It is the tester's job to enabled the calls that they are interested in
  8. testing, all calls where the return value of the call and side effects are not
  9. recorded (logging, for example) are likely to succeed w/o effort.
  10.  
  11. If you wish to call the actual implementation of a function on a MockObject,
  12. you have to enable it using enableMethod.  If you wish to use an actual variable setting, you need to set it.
  13.  
  14. All enabling/checking methods for a MockObject are done through the _mock attribute.  Example:
  15.  
  16. class Foo(object):
  17.     def __init__(self):
  18.         # NOTE: this initialization is not called by default with the mock
  19.         # object.
  20.         self.one = 'a'
  21.         self.two = 'b'
  22.  
  23.     def method(self, param):
  24.         # this method is enabled by calling _mock.enableMethod
  25.         param.bar('print some data')
  26.         self.printMe('some other data', self.one)
  27.         return self.two
  28.  
  29.     def printMe(self, otherParam):
  30.         # this method is not enabled and so is stubbed out in the MockInstance.
  31.         print otherParam
  32.  
  33. def test():
  34.     m = MockInstance(Foo)
  35.     m._mock.set(two=123)
  36.     m._mock.enableMethod('method')
  37.     param = MockObject()
  38.     rv = m.method(param)
  39.     assert(rv == 123) #m.two is returned
  40.     # note that param.bar is created on the fly as it is accessed, and
  41.     # stores how it was called.
  42.     assert(param.bar._mock.assertCalled('print some data')
  43.     # m.one and m.printMe were created on the fly as well
  44.     # m.printMe remembers how it was called.
  45.     m.printMe._mock.assertCalled('some other data', m.one)
  46.     # attribute values are generated on the fly but are retained between
  47.     # accesses.
  48.     assert(m.foo is m.foo)
  49.  
  50. TODO: set the return values for particular function calls w/ particular
  51. parameters.

advertising

Create a Paste

Please enter your new post below (or upload a file instead):





Please note that information posted here will not expire by default. If you want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords.

fantasy-obligation