沃梦达 / 编程问答 / php问题 / 正文

使用 PHPUnit 模拟私有方法

Mock private method with PHPUnit(使用 PHPUnit 模拟私有方法)

本文介绍了使用 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 模拟私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 PHPUnit 模拟私有方法

基础教程推荐