<legend id='5KFYA'><style id='5KFYA'><dir id='5KFYA'><q id='5KFYA'></q></dir></style></legend>

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

      <bdo id='5KFYA'></bdo><ul id='5KFYA'></ul>
      <tfoot id='5KFYA'></tfoot>
    1. <small id='5KFYA'></small><noframes id='5KFYA'>

    2. 使用 PHPUnit 模拟私有方法

      时间:2023-10-31
    3. <legend id='L0eCC'><style id='L0eCC'><dir id='L0eCC'><q id='L0eCC'></q></dir></style></legend>
        <bdo id='L0eCC'></bdo><ul id='L0eCC'></ul>

          <tbody id='L0eCC'></tbody>
      • <small id='L0eCC'></small><noframes id='L0eCC'>

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

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

                本文介绍了使用 PHPUnit 模拟私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个关于使用 PHPUnit 在类中模拟私有方法的问题.举个例子来介绍一下:

                I have a question about using PHPUnit to mock a private method inside a class. Let me introduce with an example:

                class A {
                  public function b() { 
                    // some code
                    $this->c(); 
                    // some more code
                  }
                
                  private function c(){ 
                    // some code
                  }
                }
                

                如何存根私有方法的结果来测试公共函数的更多代码部分.

                How can I stub the result of the private method to test the some more code part of the public function.

                解决了部分阅读这里

                推荐答案

                通常你只是不测试或模拟私有 &直接受保护的方法.

                Usually you just don't test or mock the private & protected methods directy.

                您要测试的是您班级的 public API.其他所有内容都是您的类的实现细节,如果您更改它,则不应破坏"您的测试.

                What you want to test is the public API of your class. Everything else is an implementation detail for your class and should not "break" your tests if you change it.

                当您注意到无法获得 100% 的代码覆盖率"时,这也会对您有所帮助,因为您的类中可能存在无法通过调用公共 API 来执行的代码.

                That also helps you when you notice that you "can't get 100% code coverage" because you might have code in your class that you can't execute by calling the public API.

                但如果你的班级是这样的:

                But if your class looks like this:

                class a {
                
                    public function b() {
                        return 5 + $this->c();
                    }
                
                    private function c() {
                        return mt_rand(1,3);
                    }
                }
                

                我可以看到需要模拟 c(),因为随机"函数是全局状态,您无法对其进行测试.

                i can see the need to want to mock out c() since the "random" function is global state and you can't test that.

                干净?/冗长?/overcomplicated-maybe?/i-like-it-通常"解决方案

                The "clean?/verbose?/overcomplicated-maybe?/i-like-it-usually" Solution

                class a {
                
                    public function __construct(RandomGenerator $foo) {
                        $this->foo = $foo;
                    }
                
                    public function b() {
                        return 5 + $this->c();
                    }
                
                    private function c() {
                        return $this->foo->rand(1,3);
                    }
                }
                

                现在不再需要模拟c()",因为它不包含任何全局变量,您可以很好地进行测试.

                now there is no more need to mock "c()" out since it does not contain any globals and you can test nicely.

                如果您不想或无法从您的私有函数中删除全局状态(坏事坏现实或您对坏的定义可能不同),您可以针对模拟.

                If you don't want to do or can't remove the global state from your private function (bad thing bad reality or you definition of bad might be different) that you can test against the mock.

                // maybe set the function protected for this to work
                $testMe = $this->getMock("a", array("c"));
                $testMe->expects($this->once())->method("c")->will($this->returnValue(123123));
                

                并针对这个模拟运行您的测试,因为您取出/模拟的唯一函数是c()".

                and run your tests against this mock since the only function you take out/mock is "c()".

                引用实用单元测试"一书:

                To quote the "Pragmatic Unit Testing" book:

                一般来说,你不想为了测试而破坏任何封装(或者就像妈妈以前说的,不要暴露你的隐私!").大多数时候,你应该能够通过执行其公共方法来测试一个类.如果有重要的功能隐藏在私有或受保护的访问后面,这可能是一个警告信号,表明那里有另一个类正在努力摆脱."

                "In general, you don't want to break any encapsulation for the sake of testing (or as Mom used to say, "don't expose your privates!"). Most of the time, you should be able to test a class by exercising its public methods. If there is significant functionality that is hidden behind private or protected access, that might be a warning sign that there's another class in there struggling to get out."

                <小时>

                更多:为什么你不想测试私有方法.

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

                上一篇:如何使用 Android 拍照并发送到 HTTP POST 请求? 下一篇:PHPUnit 中的模拟 - 具有不同参数的同一方法的多个配置

                相关文章

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

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

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

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