具有名称空间的PHPUnit getMock() [英] PHPUnit getMock() with namespace

查看:83
本文介绍了具有名称空间的PHPUnit getMock()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在PHP中开始使用名称空间的要求一直在继续.这次,PHPUnit给我带来了问题.我的setup()方法是这样的:

My quest of starting to use namespaces in PHP keeps continuing. This time PHPUnit gives me problems. My setup() method is like this:

$test = new \MyNamespace\NonPersistentStorage(); // works
$mock = $this->getMock('\\MyNamespace\\NonPersistentStorage'); // doesn't work

getMock()方法仅导致PHP查找NonPersistentStorage类.不在名称空间之内.

The getMock() method only results in PHP looking for a NonPersistentStorage class. Not within the namespace.

问:我需要更改什么才能使getMock()方法在名称空间中查找类?

Q: What do I need to change to get the getMock() method look for the class in the namespace?

双反斜杠不是问题.另请参见:手册 :(引用)

The double backslash is not the problem. Also see: the manual: (quote)

'在单引号内的字符串中,反斜杠转义序列使用起来更安全,但仍建议实践中将所有字符串中的反斜杠转义为最佳做法.'

'Inside a single-quoted string, the backslash escape sequence is much safer to use, but it is still recommended practice to escape backslashes in all strings as a best practice.'

对我有用的是Ignace R的答案的评论.

edit: What worked for me is in the comments of the answer of Ignace R.

推荐答案

对类的字符串引用通常没有反斜杠.尝试将其删除并告诉我们是否可行.

String references to classes generally don't have the leading backslash. Try removing it and tell us if it works.

,如果没有,请尝试

and if it doesn't, try class_alias to create an alias in the global namespace for that class. However, that would be an ugly solution...

这篇关于具有名称空间的PHPUnit getMock()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆