Line 1 was replaced by line 1 |
- This is a small one. |
+ This is an automation of a testing pattern for injecting a MockObject into a class under test when it normally creates such an object internally. |
Line 3 was replaced by line 3 |
- Using the template method pattern makes testing easier. Simply create a test version of the (abstract?) class with the key method overridden. In particular, if the method is a factory one then this can be a way of injecting a MockObject into the class. |
+ In brief... |
Lines 5-13 were replaced by line 5 |
- The type of test method you want is usually similar to the type you want in a generated mock, so it is no surprise that we have added this to our homebrew PHP mocks library. The following code... |
- {{{ |
- mock::generate_partial("my_class", "test_version_of_my_class", array("this_method", "that_method")); |
- }}} |
- ...allows us to create a new object... |
- {{{ |
- $test_object = new test_version_of_my_class(); |
- }}} |
- ...and to set return values and record calls to this_method() and that_method(). Other methods remain the same of course as it is just a simple inheritance. |
+ One way of getting a mock into an object that itself creates another object is to move the creation to a separate method. When testing, the class under test is subclassed and the creation method overridden to create the desired mock instead. The most flexible version of this is one where the overridden method can have expectations and returns set just like a mock. As the method overlay is itself an act of mocking (in this case only one method) I called it partial mocking. |
Line 15 was replaced by line 7 |
- There are two purposes to this. One is testing the class with the templated method itself. The other more common one is where we require a mock with quite a lot of real behaviour. Usually this is from some kind of localised integration test. Now the partially mocked class is a test tool rather than the class under test. |
+ Like mock objects, this is susceptible to automation using generation for the subclass. The PHP SimpleTest unit tester includes such a code generator and so I have explained it in more detail here [http://www.lastcraft.com/partial_mocks_documenation.php]. |
Line 17 was replaced by line 9 |
- There is more discussion of this at MockObjectsAndDemeter. |
+ Is this a good idea? I only really use PartialMocks for mock injection, although it could be extended for testing less than a class. Breaking class level testing kind of degrades the OO style upgrade you get from using MockObjects. When used for getting mocks into objects it is a compromise between adding extra optional parameters (or overloaded methods) which produces very visible test code, and passing in factories for all creation which could trigger a long chain of refactorings. This last solution is the most highly factored (and favoured by SteveFreeman), but isn't sometimes overkill? |
Line 19 was replaced by line 11 |
- In 1899 unit tests (up to today) we have used this about 7 times. We have used it in integration tests to reduce test data about the same number of times. Not a great deal of use, but it has paid for it's small initial investment. - MarcuS |
+ - MarcuS |