如何使用 PHPUnit 从头开始创建一个假的 class?
How to create a fake class from scratch with PHPUnit?
我目前需要测试一种方法,该方法检查 class 是否存在使用 PHPUnit 提供的方法。
目前,我只是在我的单元测试中创建一个空的 class,它工作正常。我猜可能有更好的解决方案。这是我目前的代码 运行:
class ControllerMiddleWareTest extends TestCase
{
/**
* Check to see if a faulty method name leads to a MethodNotFoundException.
*/
public function testShouldMethodNotFoundException()
{
$this->expectException(MethodNotFoundException::class);
$parameterBag = new Bag(['lang' => 'NL']);
// This class name(and method) should never exist.
new ControllerMiddleware('AwesomeController', 'init', $parameterBag);
}
}
// Mock class so we get a MethodNotFoundException.
class AwesomeController{}
我发现了很多例子,其中现有的 class 被嘲笑,但没有像这样创建 class 的地方。我将如何正确地执行此操作?
找到解决方案:
$this->getMockBuilder('AwesomeController')->getMock();
如果有人知道更好的方法或有任何建议,我将很高兴听到。
我目前需要测试一种方法,该方法检查 class 是否存在使用 PHPUnit 提供的方法。
目前,我只是在我的单元测试中创建一个空的 class,它工作正常。我猜可能有更好的解决方案。这是我目前的代码 运行:
class ControllerMiddleWareTest extends TestCase
{
/**
* Check to see if a faulty method name leads to a MethodNotFoundException.
*/
public function testShouldMethodNotFoundException()
{
$this->expectException(MethodNotFoundException::class);
$parameterBag = new Bag(['lang' => 'NL']);
// This class name(and method) should never exist.
new ControllerMiddleware('AwesomeController', 'init', $parameterBag);
}
}
// Mock class so we get a MethodNotFoundException.
class AwesomeController{}
我发现了很多例子,其中现有的 class 被嘲笑,但没有像这样创建 class 的地方。我将如何正确地执行此操作?
找到解决方案:
$this->getMockBuilder('AwesomeController')->getMock();
如果有人知道更好的方法或有任何建议,我将很高兴听到。