用PHPUnit模拟私有方法 [英] Mock private method with PHPUnit

查看:78
本文介绍了用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.

此处阅读了部分内容

Solved partially reading here

推荐答案

通常,您只是不测试或嘲笑私人&直接保护方法.

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.

干净?/冗长?/过于复杂?也许?/我喜欢吗?"解决方案

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


更多: Why you don't want test private methods.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆