如何使用Phpunit代理原始方法并同时禁用构造函数?
发布时间:2020-12-13 17:47:49 所属栏目:PHP教程 来源:网络整理
导读:使用Phpunit 4.5.2,我试图模拟以下类: class Foo { public function bar() {}}class MyClass{ private $foo; public function __construct(Foo $foo) { $this-foo = $foo; //some other stuff that I want to suppress during the unit tests. } public fun
使用Phpunit 4.5.2,我试图模拟以下类:
class Foo { public function bar() {} } class MyClass { private $foo; public function __construct(Foo $foo) { $this->foo = $foo; //some other stuff that I want to suppress during the unit tests. } public function doSomething() { $this->foo->bar(); } } 我希望实现以下目标: >让模拟调用原始方法. 这段代码: $mock = $this->getMockBuilder('MyClass') ->disableOriginalConstructor() ->enableProxyingToOriginalMethods() ->getMock() 将失败并显示以下错误消息:
如果我删除了enableProxyingToOriginalMethods(),则创建的模拟没有错误,所以当我启用代理时,这启用了构造函数,尽管调用了disableOriginalConstructor(). 如何在保持构造函数禁用的同时启用代理? 解决方法
如果您代理原始类,则必须实例化原始类的对象.如果原始类具有构造函数,则必须执行该构造函数.因此disableOriginalConstructor()和enableProxyingToOriginalMethods()是互斥的.
随意在https://github.com/sebastianbergmann/phpunit-mock-objects/issues打开一张票,要求PHPUnit在这两个一起使用时发出错误. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |