<i id='1WE6X'><tr id='1WE6X'><dt id='1WE6X'><q id='1WE6X'><span id='1WE6X'><b id='1WE6X'><form id='1WE6X'><ins id='1WE6X'></ins><ul id='1WE6X'></ul><sub id='1WE6X'></sub></form><legend id='1WE6X'></legend><bdo id='1WE6X'><pre id='1WE6X'><center id='1WE6X'></center></pre></bdo></b><th id='1WE6X'></th></span></q></dt></tr></i><div id='1WE6X'><tfoot id='1WE6X'></tfoot><dl id='1WE6X'><fieldset id='1WE6X'></fieldset></dl></div>

  1. <legend id='1WE6X'><style id='1WE6X'><dir id='1WE6X'><q id='1WE6X'></q></dir></style></legend>

      <small id='1WE6X'></small><noframes id='1WE6X'>

      <tfoot id='1WE6X'></tfoot>
        <bdo id='1WE6X'></bdo><ul id='1WE6X'></ul>

      phpunit mock 方法多次调用不同参数

      时间:2023-10-31

          <i id='2pmRA'><tr id='2pmRA'><dt id='2pmRA'><q id='2pmRA'><span id='2pmRA'><b id='2pmRA'><form id='2pmRA'><ins id='2pmRA'></ins><ul id='2pmRA'></ul><sub id='2pmRA'></sub></form><legend id='2pmRA'></legend><bdo id='2pmRA'><pre id='2pmRA'><center id='2pmRA'></center></pre></bdo></b><th id='2pmRA'></th></span></q></dt></tr></i><div id='2pmRA'><tfoot id='2pmRA'></tfoot><dl id='2pmRA'><fieldset id='2pmRA'></fieldset></dl></div>

          <small id='2pmRA'></small><noframes id='2pmRA'>

            <tbody id='2pmRA'></tbody>

          <tfoot id='2pmRA'></tfoot>
          <legend id='2pmRA'><style id='2pmRA'><dir id='2pmRA'><q id='2pmRA'></q></dir></style></legend>
          • <bdo id='2pmRA'></bdo><ul id='2pmRA'></ul>
                本文介绍了phpunit mock 方法多次调用不同参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有没有办法为不同的输入参数定义不同的模拟期望?例如,我有一个名为 DB 的数据库层类.此类具有称为Query (string $query)"的方法,该方法在输入时采用 SQL 查询字符串.我可以为这个类(DB)创建模拟并为取决于输入查询字符串的不同查询方法调用设置不同的返回值吗?

                Is there any way to define different mock-expects for different input arguments? For example, I have database layer class called DB. This class has method called "Query ( string $query )", that method takes an SQL query string on input. Can I create mock for this class (DB) and set different return values for different Query method calls that depends on input query string?

                推荐答案

                PHPUnit Mocking 库(默认)仅根据传递给 expects 参数的匹配器和传递的约束来确定期望是否匹配方法.因此,两个仅在传递给 with 的参数方面不同的 expect 调用将失败,因为两者都会匹配,但只有一个会验证是否具有预期的行为.见实际工作示例后的复现案例.

                The PHPUnit Mocking library (by default) determines whether an expectation matches based solely on the matcher passed to expects parameter and the constraint passed to method. Because of this, two expect calls that only differ in the arguments passed to with will fail because both will match but only one will verify as having the expected behavior. See the reproduction case after the actual working example.

                对于您的问题,您需要使用 ->at()->will($this->returnCallback(关于这个主题的另一个问题.

                For you problem you need to use ->at() or ->will($this->returnCallback( as outlined in another question on the subject.

                <?php
                
                class DB {
                    public function Query($sSql) {
                        return "";
                    }
                }
                
                class fooTest extends PHPUnit_Framework_TestCase {
                
                
                    public function testMock() {
                
                        $mock = $this->getMock('DB', array('Query'));
                
                        $mock
                            ->expects($this->exactly(2))
                            ->method('Query')
                            ->with($this->logicalOr(
                                 $this->equalTo('select * from roles'),
                                 $this->equalTo('select * from users')
                             ))
                            ->will($this->returnCallback(array($this, 'myCallback')));
                
                        var_dump($mock->Query("select * from users"));
                        var_dump($mock->Query("select * from roles"));
                    }
                
                    public function myCallback($foo) {
                        return "Called back: $foo";
                    }
                }
                

                转载:

                phpunit foo.php
                PHPUnit 3.5.13 by Sebastian Bergmann.
                
                string(32) "Called back: select * from users"
                string(32) "Called back: select * from roles"
                .
                
                Time: 0 seconds, Memory: 4.25Mb
                
                OK (1 test, 1 assertion)
                



                重现为什么两个 ->with() 调用不起作用:

                <?php
                
                class DB {
                    public function Query($sSql) {
                        return "";
                    }
                }
                
                class fooTest extends PHPUnit_Framework_TestCase {
                
                
                    public function testMock() {
                
                        $mock = $this->getMock('DB', array('Query'));
                        $mock
                            ->expects($this->once())
                            ->method('Query')
                            ->with($this->equalTo('select * from users'))
                            ->will($this->returnValue(array('fred', 'wilma', 'barney')));
                
                        $mock
                            ->expects($this->once())
                            ->method('Query')
                            ->with($this->equalTo('select * from roles'))
                            ->will($this->returnValue(array('admin', 'user')));
                
                        var_dump($mock->Query("select * from users"));
                        var_dump($mock->Query("select * from roles"));
                    }
                
                }
                

                结果

                 phpunit foo.php
                PHPUnit 3.5.13 by Sebastian Bergmann.
                
                F
                
                Time: 0 seconds, Memory: 4.25Mb
                
                There was 1 failure:
                
                1) fooTest::testMock
                Failed asserting that two strings are equal.
                --- Expected
                +++ Actual
                @@ @@
                -select * from roles
                +select * from users
                
                /home/.../foo.php:27
                
                FAILURES!
                Tests: 1, Assertions: 0, Failures: 1
                

                这篇关于phpunit mock 方法多次调用不同参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何让 PHPUnit MockObjects 根据参数返回不同的值? 下一篇:PHPUnit:如何使用多个参数模拟多个方法调用?

                相关文章

                  1. <tfoot id='248Kl'></tfoot><legend id='248Kl'><style id='248Kl'><dir id='248Kl'><q id='248Kl'></q></dir></style></legend>
                      <bdo id='248Kl'></bdo><ul id='248Kl'></ul>

                  2. <small id='248Kl'></small><noframes id='248Kl'>

                  3. <i id='248Kl'><tr id='248Kl'><dt id='248Kl'><q id='248Kl'><span id='248Kl'><b id='248Kl'><form id='248Kl'><ins id='248Kl'></ins><ul id='248Kl'></ul><sub id='248Kl'></sub></form><legend id='248Kl'></legend><bdo id='248Kl'><pre id='248Kl'><center id='248Kl'></center></pre></bdo></b><th id='248Kl'></th></span></q></dt></tr></i><div id='248Kl'><tfoot id='248Kl'></tfoot><dl id='248Kl'><fieldset id='248Kl'></fieldset></dl></div>