• <bdo id='JLOB6'></bdo><ul id='JLOB6'></ul>
    <legend id='JLOB6'><style id='JLOB6'><dir id='JLOB6'><q id='JLOB6'></q></dir></style></legend>
  • <small id='JLOB6'></small><noframes id='JLOB6'>

    <tfoot id='JLOB6'></tfoot>

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

      2. 如何让 PHPUnit MockObjects 根据参数返回不同的值?

        时间:2023-10-31
          <tbody id='vdleV'></tbody>
          <bdo id='vdleV'></bdo><ul id='vdleV'></ul>

            • <small id='vdleV'></small><noframes id='vdleV'>

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

                <tfoot id='vdleV'></tfoot><legend id='vdleV'><style id='vdleV'><dir id='vdleV'><q id='vdleV'></q></dir></style></legend>

                  本文介绍了如何让 PHPUnit MockObjects 根据参数返回不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 PHPUnit 模拟对象,它返回 'return value' 无论它的参数是什么:

                  I've got a PHPUnit mock object that returns 'return value' no matter what its arguments:

                  // From inside a test...
                  $mock = $this->getMock('myObject', 'methodToMock');
                  $mock->expects($this->any))
                       ->method('methodToMock')
                       ->will($this->returnValue('return value'));
                  

                  我希望能够根据传递给模拟方法的参数返回不同的值.我尝试过类似的方法:

                  What I want to be able to do is return a different value based on the arguments passed to the mock method. I've tried something like:

                  $mock = $this->getMock('myObject', 'methodToMock');
                  
                  // methodToMock('one')
                  $mock->expects($this->any))
                       ->method('methodToMock')
                       ->with($this->equalTo('one'))
                       ->will($this->returnValue('method called with argument "one"'));
                  
                  // methodToMock('two')
                  $mock->expects($this->any))
                       ->method('methodToMock')
                       ->with($this->equalTo('two'))
                       ->will($this->returnValue('method called with argument "two"'));
                  

                  但是如果没有使用参数 'two' 调用 mock,这会导致 PHPUnit 抱怨,所以我假设 methodToMock('two') 的定义覆盖第一个的定义.

                  But this causes PHPUnit to complain if the mock isn't called with the argument 'two', so I assume that the definition of methodToMock('two') overwrites the definition of the first.

                  所以我的问题是:有没有办法让 PHPUnit 模拟对象根据其参数返回不同的值?如果有,怎么做?

                  So my question is: Is there any way to get a PHPUnit mock object to return a different value based on its arguments? And if so, how?

                  推荐答案

                  使用回调.例如(直接来自 PHPUnit 文档):

                  Use a callback. e.g. (straight from PHPUnit documentation):

                  <?php
                  class StubTest extends PHPUnit_Framework_TestCase
                  {
                      public function testReturnCallbackStub()
                      {
                          $stub = $this->getMock(
                            'SomeClass', array('doSomething')
                          );
                  
                          $stub->expects($this->any())
                               ->method('doSomething')
                               ->will($this->returnCallback('callback'));
                  
                          // $stub->doSomething() returns callback(...)
                      }
                  }
                  
                  function callback() {
                      $args = func_get_args();
                      // ...
                  }
                  ?>
                  

                  在 callback() 中做任何你想做的处理,并根据你的 $args 适当地返回结果.

                  Do whatever processing you want in the callback() and return the result based on your $args as appropriate.

                  这篇关于如何让 PHPUnit MockObjects 根据参数返回不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHPUnit 中的模拟 - 具有不同参数的同一方法的多个配置 下一篇:phpunit mock 方法多次调用不同参数

                  相关文章

                  1. <tfoot id='VWAJT'></tfoot>
                      <bdo id='VWAJT'></bdo><ul id='VWAJT'></ul>

                    <small id='VWAJT'></small><noframes id='VWAJT'>

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

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