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

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

        使用phpunit在抽象类中模拟具体方法

        Mocking concrete method in abstract class using phpunit(使用phpunit在抽象类中模拟具体方法)
      4. <tfoot id='2UQch'></tfoot>
          <tbody id='2UQch'></tbody>

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

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

                  <legend id='2UQch'><style id='2UQch'><dir id='2UQch'><q id='2UQch'></q></dir></style></legend>
                  本文介绍了使用phpunit在抽象类中模拟具体方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有什么好的方法可以使用 PHPUnit 模拟抽象类中的具体方法?

                  Are there any good ways to mock concrete methods in abstract classes using PHPUnit?

                  到目前为止我发现的是:

                  • expects()->will() 使用抽象方法可以正常工作
                  • 它不适用于具体方法.而是运行原始方法.
                  • 使用 mockbuilder 并将所有抽象方法和具体方法提供给 setMethods() 有效.但是,它需要您指定所有抽象方法,这使得测试变得脆弱且过于冗长.
                  • MockBuilder::getMockForAbstractClass() 忽略 setMethod().


                  以下是一些对上述几点进行示例的单元测试:


                  Here are some unit tests examplifying the above points:

                  abstract class AbstractClass {
                      public function concreteMethod() {
                          return $this->abstractMethod();
                      }
                  
                      public abstract function abstractMethod();
                  }
                  
                  
                  class AbstractClassTest extends PHPUnit_Framework_TestCase {
                      /**
                       * This works for abstract methods.
                       */
                      public function testAbstractMethod() {
                          $stub = $this->getMockForAbstractClass('AbstractClass');
                          $stub->expects($this->any())
                                  ->method('abstractMethod')
                                  ->will($this->returnValue(2));
                  
                          $this->assertSame(2, $stub->concreteMethod()); // Succeeds
                      }
                  
                      /**
                       * Ideally, I would like this to work for concrete methods too.
                       */
                      public function testConcreteMethod() {
                          $stub = $this->getMockForAbstractClass('AbstractClass');
                          $stub->expects($this->any())
                                  ->method('concreteMethod')
                                  ->will($this->returnValue(2));
                  
                          $this->assertSame(2, $stub->concreteMethod()); // Fails, concreteMethod returns NULL
                      }
                  
                      /**
                       * One way to mock the concrete method, is to use the mock builder,
                       * and set the methods to mock.
                       *
                       * The downside of doing it this way, is that all abstract methods
                       * must be specified in the setMethods() call. If you add a new abstract
                       * method, all your existing unit tests will fail.
                       */
                      public function testConcreteMethod__mockBuilder_getMock() {
                          $stub = $this->getMockBuilder('AbstractClass')
                                  ->setMethods(array('concreteMethod', 'abstractMethod'))
                                  ->getMock();
                          $stub->expects($this->any())
                                  ->method('concreteMethod')
                                  ->will($this->returnValue(2));
                  
                          $this->assertSame(2, $stub->concreteMethod()); // Succeeds
                      }
                  
                      /**
                       * Similar to above, but using getMockForAbstractClass().
                       * Apparently, setMethods() is ignored by getMockForAbstractClass()
                       */
                      public function testConcreteMethod__mockBuilder_getMockForAbstractClass() {
                          $stub = $this->getMockBuilder('AbstractClass')
                                  ->setMethods(array('concreteMethod'))
                                  ->getMockForAbstractClass();
                          $stub->expects($this->any())
                                  ->method('concreteMethod')
                                  ->will($this->returnValue(2));
                  
                          $this->assertSame(2, $stub->concreteMethod()); // Fails, concreteMethod returns NULL
                      }
                  }
                  

                  推荐答案

                  2年前有一个Pull Request,但从未在文档中添加信息:https://github.com/sebastianbergmann/phpunit-mock-objects/pull/49

                  There was a Pull Request for this 2 years ago, but the information never been added in the documentation : https://github.com/sebastianbergmann/phpunit-mock-objects/pull/49

                  您可以在 getMockForAbstractClass() 的参数 7 中的数组中传递具体方法.

                  You can pass your concrete method in an array in argument 7 of getMockForAbstractClass().

                  查看代码:https://github.com/andreaswolf/phpunit-mock-objects/blob/30ee7452caaa09c46421379861b4128ef7d95e2f/PHPUnit/Framework/MockObject/Generator.php#L225

                  这篇关于使用phpunit在抽象类中模拟具体方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='alO4j'></tbody>

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

                  <legend id='alO4j'><style id='alO4j'><dir id='alO4j'><q id='alO4j'></q></dir></style></legend>

                        1. <tfoot id='alO4j'></tfoot>

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