|
|
|
| Reporting | Simple Test PHP Unit Test Framework | Web tester |
The default behaviour of the mock in SimpleTest is either an identical match on the argument or to allow any argument at all. For almost all tests this is sufficient. Sometimes, though, you want to weaken a test case.
One place where a test can be too tightly coupled is with text matching. Suppose we have a component that outputs a helpful error message when something goes wrong. You want to test that the correct error was sent, but the actual text may be rather long. If you test for the text exactly, then every time the exact wording of the message changes, you will have to go back and edit the test suite.
For example, suppose we have a news service that has failed to connect to its remote source.
To get around this, we would like to do a regular expression test rather than an exact match. We can actually do this with...
The PatternExpectation takes the regular expression to match in its constructor. Whenever a comparison is made by the MockWriter against this expectation class, it will do a preg_match() with this pattern. With our test case above, as long as "cannot connect" appears in the text of the string, the mock will issue a pass to the unit tester. The rest of the text does not matter.
The possible expectation classes are...
| AnythingExpectation | Will always match |
| EqualExpectation | An equality, rather than the stronger identity comparison |
| NotEqualExpectation | An inequality comparison |
| IndenticalExpectation | The default mock object check which must match exactly |
| NotIndenticalExpectation | Inverts the mock object logic |
| WithinMarginExpectation | Compares a value to within a margin |
| OutsideMarginExpectation | Checks that a value is out side the margin |
| PatternExpectation | Uses a Perl Regex to match a string |
| NoPatternExpectation | Passes only if failing a Perl Regex |
| IsAExpectation | Checks the type or class name only |
| NotAExpectation | Opposite of the IsAExpectation |
| MethodExistsExpectation | Checks a method is available on an object |
| TrueExpectation | Accepts any PHP variable that evaluates to true |
| FalseExpectation | Accepts any PHP variable that evaluates to false |
Some examples...
The expectation classes can be used not just for sending assertions from mock objects, but also for selecting behaviour for the mock. Anywhere a list of arguments is given, a list of expectation objects can be inserted instead.
Suppose we want a mock authorisation server to simulate a successful login, but only if it receives a valid session object. We can do this as follows...
This kind of sophistication is rarely useful, but is included for completeness.
The expectation classes have a very simple structure. So simple that it is easy to create your own versions for commonly used test logic.
As an example here is the creation of a class to test for valid IP addresses. In order to work correctly with the stubs and mocks the new expectation class should extend SimpleExpectation or further extend a subclass...
This class can now be used in place of the earlier expectation classes.
Here is a more typical example, matching part of a hash...
Suppose some authenticator is expecting to be given a database row corresponding to the user, and we only need to confirm the username is correct. We can assert just their username with...
The SimpleTest unit testing framework also uses the expectation classes internally for the class.html UnitTestCase class. We can also take advantage of these mechanisms to reuse our homebrew expectation classes within the test suites directly.
The most crude way of doing this is to use the generic SimpleTest::assert() method to test against it directly...
This is a little untidy compared with our usual assert...() syntax.
For such a simple case we would normally create a separate assertion method on our test case rather than bother using the expectation class. If we pretend that our expectation is a little more complicated for a moment, so that we want to reuse it, we get...
|
|
|
| Reporting | Simple Test PHP Unit Test Framework | Web tester |
Documentation generated on Sun, 31 Oct 2010 16:30:47 -0500 by phpDocumentor 1.4.3