dtcu5885
2011-05-16 10:04
浏览 22
已采纳

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

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?

图片转代码服务由CSDN问答提供 功能建议

尝试为需要Zend_Config对象类型的方法执行Zend_Config的简单模拟,但mock返回一个类型 Mock_Zend_Config。

当然我在这么晚的时候错过了一些东西,我在函数调用中显然是错的,但我没有发现我的错误。

   $ config = $ this-> getMock(“Zend_Config”);  
   
 
 

返回Mock_Zend_Config,我的对象需要是Zend_Config类型。 在备忘单中查找函数签名并将方法调用更改为:

  $ config = $ this-> getMock(“Zend_Config”,array(),array($  confArray), “Zend_Config的”,真正的);  
   
 
 

此版本生成致命错误消息“Zend_Config已存在”。

在旁注中,可能与 phpunit就是这样,但是typehint不会产生致命错误,并且在没有测试的情况下运行。

知道我在la mock中缺少什么吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzi0609 2011-05-16 10:13
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题