为什么模拟对象有错误的类型提示?

发布于 2024-11-07 15:19:06 字数 537 浏览 6 评论 0原文

尝试对需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍景流年 2024-11-14 15:19:06

模拟对象扩展了模拟对象。任何扩展 Zend_Config 的类都会满足 Zend_Config 的类型提示,因为根据定义 Mock_Zend_Config extends Zend_Config 因此 Zend_Config。因此,当您尝试将模拟命名为它扩展的类时,您将收到致命错误,如果不这样做,则不会出现致命错误。

Mock objects extend the mocked object. A type hint for Zend_Config will be satisfied by any class extending Zend_Config because by definition a Mock_Zend_Config extends Zend_Config and therefor is a Zend_Config. Consequently, you will get a Fatal Error when trying to name the mock like the class it extends and none if you dont.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文