为什么模拟对象有错误的类型提示?
尝试对需要 Zend_Config 对象类型的方法进行 Zend_Config 的简单模拟,但模拟返回 Mock_Zend_Config 类型。
当然,这么晚了我错过了一些东西,而且我在函数调用中显然是错误的,但我没有发现我的错误。
$config = $this->getMock("Zend_Config");
返回 Mock_Zend_Config,并且我的对象需要是 Zend_Config 类型。在备忘单中查找函数签名并将方法调用更改为:
$config = $this->getMock("Zend_Config", array(), array($confArray),"Zend_Config",true);
此版本生成致命错误,并显示消息“Zend_Config 已存在”。
顺便说一句,可能与 phpunit 无关,但 typehint 不会产生致命错误,因为它应该,并且在没有测试的情况下运行时会产生致命错误。
知道我在模拟中缺少什么吗?
Trying to do a simple mock of Zend_Config for a method that requires a Zend_Config object type, but the mock returns a type of Mock_Zend_Config.
Surely I missed something at this late hour and I am obviously wrong in the function call but I fail to spot my error.
$config = $this->getMock("Zend_Config");
Returns Mock_Zend_Config, and my object needs to be of type Zend_Config. Looked up the function signature in a cheatsheet and changed the method call to:
$config = $this->getMock("Zend_Config", array(), array($confArray),"Zend_Config",true);
This version generates a fatal error with message "Zend_Config already exists".
On a sidenote and probably not related to phpunit as such but the typehint does not generate a fatal error as it should , and does so when run without tests.
Any idea of what I'm missing in la mock?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模拟对象扩展了模拟对象。任何扩展
Zend_Config
的类都会满足Zend_Config
的类型提示,因为根据定义Mock_Zend_Config
extendsZend_Config
因此是Zend_Config
。因此,当您尝试将模拟命名为它扩展的类时,您将收到致命错误,如果不这样做,则不会出现致命错误。Mock objects extend the mocked object. A type hint for
Zend_Config
will be satisfied by any class extendingZend_Config
because by definition aMock_Zend_Config
extendsZend_Config
and therefor is aZend_Config
. Consequently, you will get a Fatal Error when trying to name the mock like the class it extends and none if you dont.