<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在抽象类中模拟具体方法

        时间:2023-10-31
      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在抽象类中模拟具体方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHPUnit - 创建 Mock 对象以充当属性的存根 下一篇:我可以依靠 PHP php.ini 精度解决方法来解决浮点问题吗

                  相关文章

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