expectation_documentation.html.svn-base

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 423 行 · 第 1/2 页

SVN-BASE
423
字号
            </p>            <p><pre>$mock-&gt;expectOnce('method', array(new IdenticalExpectation(14)));</pre>                This is the same as <span class="new_code">$mock-&gt;expectOnce('method', array(14))</span>.<pre>$mock-&gt;expectOnce('method', array(new EqualExpectation(14)));</pre>                This is different from the previous version in that the string                <span class="new_code">"14"</span> as a parameter will also pass.                Sometimes the additional type checks of SimpleTest are too restrictive.<pre>$mock-&gt;expectOnce('method', array(new AnythingExpectation(14)));</pre>                This is the same as <span class="new_code">$mock-&gt;expectOnce('method', array('*'))</span>.<pre>$mock-&gt;expectOnce('method', array(new IdenticalExpectation('*')));</pre>                This is handy if you want to assert a literal <span class="new_code">"*"</span>.<pre>new NotIdenticalExpectation(14)</pre>                This matches on anything other than integer 14.                Even the string <span class="new_code">"14"</span> would pass.<pre>new WithinMarginExpectation(14.0, 0.001)</pre>                This will accept any value from 13.999 to 14.001 inclusive.            </p>                <p><a class="target" name="behaviour"><h2>Using expectations to control stubs</h2></a></p>            <p>                The expectation classes can be used not just for sending assertions                from mock objects, but also for selecting behaviour for the                <a href="mock_objects_documentation.html">mock objects</a>.                Anywhere a list of arguments is given, a list of expectation objects                can be inserted instead.            </p>            <p>                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...<pre>Mock::generate('Authorisation');<strong>$authorisation = new MockAuthorisation();$authorisation-&gt;setReturnValue(        'isAllowed',        true,        array(new IsAExpectation('Session', 'Must be a session')));$authorisation-&gt;setReturnValue('isAllowed', false);</strong></pre>                We have set the default mock behaviour to return false when                <span class="new_code">isAllowed</span> is called.                When we call the method with a single parameter that                is a <span class="new_code">Session</span> object, it will return true.                We have also added a second parameter as a message.                This will be displayed as part of the mock object                failure message if this expectation is the cause of                a failure.            </p>            <p>                This kind of sophistication is rarely useful, but is included for                completeness.            </p>                <p><a class="target" name="extending"><h2>Creating your own expectations</h2></a></p>            <p>                The expectation classes have a very simple structure.                So simple that it is easy to create your own versions for                commonly used test logic.            </p>            <p>                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                <span class="new_code">SimpleExpectation</span>...<pre><strong>class ValidIp extends SimpleExpectation {        function test($ip) {        return (ip2long($ip) != -1);    }        function testMessage($ip) {        return "Address [$ip] should be a valid IP address";    }}</strong></pre>                There are only two methods to implement.                The <span class="new_code">test()</span> method should                evaluate to true if the expectation is to pass, and                false otherwise.                The <span class="new_code">testMessage()</span> method                should simply return some helpful text explaining the test                that was carried out.            </p>            <p>                This class can now be used in place of the earlier expectation                classes.            </p>                <p><a class="target" name="unit"><h2>Under the bonnet of the unit tester</h2></a></p>            <p>                The <a href="http://sourceforge.net/projects/simpletest/">SimpleTest unit testing framework</a>                also uses the expectation classes internally for the                <a href="unit_test_documentation.html">UnitTestCase class</a>.                We can also take advantage of these mechanisms to reuse our                homebrew expectation classes within the test suites directly.            </p>            <p>                The most crude way of doing this is to use the                <span class="new_code">SimpleTest::assert()</span> method to                test against it directly...<pre><strong>class TestOfNetworking extends UnitTestCase {    ...    function testGetValidIp() {        $server = &amp;new Server();        $this-&gt;assert(                new ValidIp(),                $server-&gt;getIp(),                'Server IP address-&gt;%s');    }}</strong></pre>                This is a little untidy compared with our usual                <span class="new_code">assert...()</span> syntax.            </p>            <p>                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...<pre>class TestOfNetworking extends UnitTestCase {    ...<strong>    function assertValidIp($ip, $message = '%s') {        $this-&gt;assert(new ValidIp(), $ip, $message);    }</strong>        function testGetValidIp() {        $server = &amp;new Server();<strong>        $this-&gt;assertValidIp(                $server-&gt;getIp(),                'Server IP address-&gt;%s');</strong>    }}</pre>                It is unlikely we would ever need this degree of control                over the testing machinery.                It is rare to need the expectations for more than pattern                matching.                Also, complex expectation classes could make the tests                harder to read and debug.                These mechanisms are really of most use to authors of systems                that will extend the test framework to create their own tool set.            </p>            </div>        References and related information...        <ul><li>            SimpleTest project page on <a href="http://sourceforge.net/projects/simpletest/">SourceForge</a>.        </li><li>            SimpleTest download page on <a href="http://www.lastcraft.com/simple_test.php">LastCraft</a>.        </li><li>            The expectations mimic the constraints in <a href="http://www.jmock.org/">JMock</a>.        </li><li>            <a href="http://simpletest.org/api/">Full API for SimpleTest</a>            from the PHPDoc.        </li></ul><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><div class="copyright">            Copyright<br>Marcus Baker 2006        </div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?