• <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 根据参数返回不同的值?

        How can I get PHPUnit MockObjects to return different values based on a parameter?(如何让 PHPUnit MockObjects 根据参数返回不同的值?)
          <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 根据参数返回不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                      <tbody id='VWAJT'></tbody>
                    <tfoot id='VWAJT'></tfoot>
                      <bdo id='VWAJT'></bdo><ul id='VWAJT'></ul>

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

                    • <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>

                          • <legend id='VWAJT'><style id='VWAJT'><dir id='VWAJT'><q id='VWAJT'></q></dir></style></legend>