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

    3. <tfoot id='S2XKG'></tfoot>
      <legend id='S2XKG'><style id='S2XKG'><dir id='S2XKG'><q id='S2XKG'></q></dir></style></legend>

        <bdo id='S2XKG'></bdo><ul id='S2XKG'></ul>

        PHPUnit - 创建 Mock 对象以充当属性的存根

        时间:2023-10-31
          • <bdo id='hU61r'></bdo><ul id='hU61r'></ul>
              <legend id='hU61r'><style id='hU61r'><dir id='hU61r'><q id='hU61r'></q></dir></style></legend>

            • <tfoot id='hU61r'></tfoot>

                <tbody id='hU61r'></tbody>

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

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

                1. 本文介绍了PHPUnit - 创建 Mock 对象以充当属性的存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 PHPunit 中配置一个 Mock 对象以返回不同属性的值(使用 __get 函数访问)

                  I'm trying to configure a Mock object in PHPunit to return values for different properties (that are accessed using the __get function)

                  例子:

                  class OriginalObject {
                   public function __get($name){
                  switch($name)
                   case "ParameterA":
                    return "ValueA";
                   case "ParameterB":
                    return "ValueB";
                   }
                  }
                  

                  我正在尝试使用:

                  $mockObject = $this->getMock("OrigionalObject");
                  
                  $mockObject    ->expects($this->once())
                      ->method('__get')
                      ->with($this->equalTo('ParameterA'))
                      ->will($this->returnValue("ValueA"));
                  
                  $mockObject    ->expects($this->once())
                      ->method('__get')
                      ->with($this->equalTo('ParameterB'))
                      ->will($this->returnValue("ValueB"));
                  

                  但这非常失败:-(

                  推荐答案

                  我还没有尝试模拟 __get,但也许这会起作用:

                  I haven't tried mocking __get yet, but maybe this will work:

                  // getMock() is deprecated
                  // $mockObject = $this->getMock("OrigionalObject");
                  $mockObject = $this->createMock("OrigionalObject");
                  
                  $mockObject->expects($this->at(0))
                      ->method('__get')
                      ->with($this->equalTo('ParameterA'))
                      ->will($this->returnValue('ValueA'));
                  
                  $mockObject->expects($this->at(1))
                      ->method('__get')
                      ->with($this->equalTo('ParameterB'))
                      ->will($this->returnValue('ValueB'));
                  

                  我已经在测试中使用了 $this->at() 并且它有效(但不是最佳解决方案).我是从这个胎面得到的:

                  I've already used $this->at() in a test and it works (but isn't an optimal solution). I got it from this tread:

                  如何我可以让 PHPUnit MockObjects 根据参数返回不同的值吗?

                  这篇关于PHPUnit - 创建 Mock 对象以充当属性的存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Laravel 中运行功能测试时如何模拟服务(或服务提供者)? 下一篇:使用phpunit在抽象类中模拟具体方法

                  相关文章

                  <small id='0HIgL'></small><noframes id='0HIgL'>

                  <tfoot id='0HIgL'></tfoot>

                    <legend id='0HIgL'><style id='0HIgL'><dir id='0HIgL'><q id='0HIgL'></q></dir></style></legend>

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