expectation_documentation.html.svn-base
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 423 行 · 第 1/2 页
SVN-BASE
423 行
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title> Extending the SimpleTest unit tester with additional expectation classes </title><link rel="stylesheet" type="text/css" href="docs.css" title="Styles"></head><body><div class="menu_back"><div class="menu"><a href="index.html">SimpleTest</a> | <a href="overview.html">Overview</a> | <a href="unit_test_documentation.html">Unit tester</a> | <a href="group_test_documentation.html">Group tests</a> | <a href="mock_objects_documentation.html">Mock objects</a> | <a href="partial_mocks_documentation.html">Partial mocks</a> | <a href="reporter_documentation.html">Reporting</a> | <span class="chosen">Expectations</span> | <a href="web_tester_documentation.html">Web tester</a> | <a href="form_testing_documentation.html">Testing forms</a> | <a href="authentication_documentation.html">Authentication</a> | <a href="browser_documentation.html">Scriptable browser</a></div></div><h1>Expectation documentation</h1> This page... <ul><li> Using expectations for <a href="#mock">more precise testing with mock objects</a> </li><li> <a href="#behaviour">Changing mock object behaviour</a> with expectations </li><li> <a href="#extending">Extending the expectations</a> </li><li> Underneath SimpleTest <a href="#unit">uses expectation classes</a> </li></ul><div class="content"> <p><a class="target" name="mock"><h2>More control over mock objects</h2></a></p> <p> The default behaviour of the <a href="mock_objects_documentation.html">mock objects</a> in <a href="http://sourceforge.net/projects/simpletest/">SimpleTest</a> 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. </p> <p> 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. </p> <p> For example, suppose we have a news service that has failed to connect to its remote source.<pre><strong>class NewsService { ... function publish(&$writer) { if (! $this->isConnected()) { $writer->write('Cannot connect to news service "' . $this->_name . '" at this time. ' . 'Please try again later.'); } ... }}</strong></pre> Here it is sending its content to a <span class="new_code">Writer</span> class. We could test this behaviour with a <span class="new_code">MockWriter</span> like so...<pre>class TestOfNewsService extends UnitTestCase { ... function testConnectionFailure() {<strong> $writer = &new MockWriter(); $writer->expectOnce('write', array( 'Cannot connect to news service ' . '"BBC News" at this time. ' . 'Please try again later.')); $service = &new NewsService('BBC News'); $service->publish($writer);</strong> }}</pre> This is a good example of a brittle test. If we decide to add additional instructions, such as suggesting an alternative news source, we will break our tests even though no underlying functionality has been altered. </p> <p> To get around this, we would like to do a regular expression test rather than an exact match. We can actually do this with...<pre>class TestOfNewsService extends UnitTestCase { ... function testConnectionFailure() { $writer = &new MockWriter();<strong> $writer->expectOnce( 'write', array(new PatternExpectation('/cannot connect/i')));</strong> $service = &new NewsService('BBC News'); $service->publish($writer); }}</pre> Instead of passing in the expected parameter to the <span class="new_code">MockWriter</span> we pass an expectation class called <span class="new_code">WantedPatternExpectation</span>. The mock object is smart enough to recognise this as special and to treat it differently. Rather than simply comparing the incoming argument to this object, it uses the expectation object itself to perform the test. </p> <p> The <span class="new_code">WantedPatternExpectation</span> takes the regular expression to match in its constructor. Whenever a comparison is made by the <span class="new_code">MockWriter</span> against this expectation class, it will do a <span class="new_code">preg_match()</span> 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. </p> <p> The possible expectation classes are... <table><tbody> <tr><td><span class="new_code">AnythingExpectation</span></td><td>Will always match</td></tr> <tr><td><span class="new_code">EqualExpectation</span></td><td>An equality, rather than the stronger identity comparison</td></tr> <tr><td><span class="new_code">NotEqualExpectation</span></td><td>An inequality comparison</td></tr> <tr><td><span class="new_code">IndenticalExpectation</span></td><td>The default mock object check which must match exactly</td></tr> <tr><td><span class="new_code">NotIndenticalExpectation</span></td><td>Inverts the mock object logic</td></tr> <tr><td><span class="new_code">WithinMarginExpectation</span></td><td>Compares a value to within a margin</td></tr> <tr><td><span class="new_code">OutsideMarginExpectation</span></td><td>Checks that a value is out side the margin</td></tr> <tr><td><span class="new_code">PatternExpectation</span></td><td>Uses a Perl Regex to match a string</td></tr> <tr><td><span class="new_code">NoPatternExpectation</span></td><td>Passes only if failing a Perl Regex</td></tr> <tr><td><span class="new_code">IsAExpectation</span></td><td>Checks the type or class name only</td></tr> <tr><td><span class="new_code">NotAExpectation</span></td><td>Opposite of the <span class="new_code">IsAExpectation</span></td></tr> <tr><td><span class="new_code">MethodExistsExpectation</span></td><td>Checks a method is available on an object</td></tr> </tbody></table> Most take the expected value in the constructor. The exceptions are the pattern matchers, which take a regular expression, and the <span class="new_code">IsAExpectation</span> and <span class="new_code">NotAExpectation</span> which takes a type or class name as a string. </p> <p> Some examples...
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?