PHPUnit:存根方法未定义 [英] PHPUnit: stub methods undefined
问题描述
我一定很想念东西.我正在尝试在PHPUnit中的类上对方法进行存根,但是当我在模拟对象上调用该方法时,它告诉我该方法未定义.
I must be missing something. I'm trying to stub methods on a class in PHPUnit, but when I invoke the method on the mock object, it tells me that method is undefined.
存根示例类:
namespace MyApp;
class MyStubClass
{
public function mrMethod()
{
// doing stuff
}
}
要存根,我写:
// specifying all getMock() args to disable calling of class __construct()
$stub = $this->getMock('MyStubClass', array(), array(), 'MockMyStubClass', false, false, false);
$stub->expects($this->any())
->method('mrMethod')
->will($this->returnValue('doing stuff'));
但是在调用存根方法时,我得到了一个例外:
But upon invoking the stubbed method, I get an exception:
$stub->mrMethod();
//PHP Fatal error: Call to undefined method MockMyStubClass::mrMethod()
我正在将PHPUnit 3.4.3与PHP 5.3.0结合使用.
I'm using PHPUnit 3.4.3 with PHP 5.3.0.
我注意到的另一件事是,如果在getMock()
方法中指定名称空间,则由于存在双重名称空间而导致类加载异常:
Another small thing I noticed was that if specifying a namespace in the getMock()
method results in a class loading exception because of a double namespace:
$stub = $this->getMock('MyApp\MyStubClass');
// Fatal error: Class 'MyApp\MyApp\MyStubClass' not found
这让我感到很奇怪(并且getmock()将不接受带有前导反斜杠的名称空间).我唯一想到的原因可能是因为该类是 在自动装带器上注册?
That strikes me as rather odd (and getmock() will not accept a namespace with a leading backslash). The only thing I could think to cause that would may be because this class is registered with an autoloader?
有什么想法吗?
推荐答案
回答我自己的问题:
经过很多挫折之后,我确实设法使事情开始了.我不确定问题到底是什么,但是确实发现了一些可能对其他人有帮助的事情:
After quite a bit of frustration, I did manage to get things working. I'm not sure precisely what the issue was, but did discover a few things that might help others:
- 确保您正在运行最新版本的PHPUnit(在撰写本文时为3.4.6)
-
使用完全限定的名称空间减去第一个反斜杠.
- Make sure you're running the latest version of PHPUnit (3.4.6 as of this writing)
Use the fully-qualified namespace minus the first backslash.
$this->getMock('MyApp\Widgets\WidgetFactory');
我的问题的一部分是PHPUnit正在创建一个存根类WidgetFactory
,而该存根类实际上并未存根MyApp\Widgets\WidgetFactory
.人们会希望,如果尝试对不存在的类进行存根,则会发生异常,但似乎不会因名称空间混乱而发生.
Part of my problem was that PHPUnit was creating a stub class WidgetFactory
that was not actually stubbing MyApp\Widgets\WidgetFactory
. One would expect that an exception would occur if trying to stub a non-existent class, but it doesn't seem to happen with the namespace confusion.
此外,在此处上存在一个问题,建议使用以下类别名方法:
Also, there is a question over here that suggests using the class alias method as follows:
class_alias('MyApp\Widgets\WidgetFactory', 'WidgetFactory');
$this->getMock('WidgetFactory');
虽然这确实可以暂时解决我的问题,但我强烈建议您不要使用它. class_alias()
对于相同的别名,不能在没有引发异常的情况下被两次调用,如果在setup()
方法中使用或作为存根生成的一部分,则会导致明显的问题.
While this did temporarily solve my problem, I would strongly advise against using it. class_alias()
cannot be called twice for the same alias without raising an exception, which causes obvious problem if used in the setup()
method, or as part of the stub generation.
这篇关于PHPUnit:存根方法未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!