在 PHPUnit 中使用 test_helpers 扩展时如何设置返回值
我最近接到一项任务,为我们的系统设置单元测试,但我遇到了一个问题。示例位于 http://sebastian-bergmann.de/archives/ 885-Stubbing-Hard-Coded-Dependency.html 的设置与我尝试测试的代码非常相似。我唯一需要做的另一件事是让 doSomethingElse()
返回我选择的内容。
更新
我尝试将getMock
设置为某个变量,然后设置返回值。但 PHPUnit 似乎没有注意到我设置的变量和返回值。这是因为,我假设该变量从未在任何地方使用过。但是根据代码的设置方式,无法将模拟对象传递到 foo 中,因此它知道使用它而不是实际的 Bar 类。我添加了 Foo 和 Bar 类以便于参考。原始的 Bar 类返回 * 但我希望它从模拟类返回 **。
对于此测试来说,使用 set_new_overload()
并不是强制的,但到目前为止,使用它是我能够成功模拟 Bar 的唯一方法。
<?php
require_once 'Foo.php';
class FooTest extends PHPUnit_Framework_TestCase {
protected function setUp()
{
$mock = $this->getMock(
'Bar', /* name of class to mock */
array('doSomethingElse'), /* list of methods to mock */
array(), /* constructor arguments */
'BarMock' /* name for mocked class */
);
$mock->expects($this->any())
->method("doSomthingElse")
->will($this->returnValue("**")
);
set_new_overload(array($this, 'newCallback'));
}
protected function tearDown()
{
unset_new_overload();
}
protected function newCallback($className)
{
switch ($className) {
case 'Bar':
return 'BarMock';
default:
return $className;
}
}
public function testDoSomething()
{
$foo = new Foo;
$this->assertTrue($foo->doSomething());
}
}
?>
<?php
require_once 'Bar.php';
class Foo {
public function doSomething()
{
// ...
$bar = new Bar;
$bar->doSomethingElse();
// ...
return TRUE;
}
}
?>
<?php
class Bar {
public function doSomethingElse()
{
return '*';
}
}
?>
I've recently been given the task of getting our system set up for unit testing but there's one problem I'm having. The example at http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html is setup very similar to the code I'm trying to test. The only other thing that I need to do is to have doSomethingElse()
return something of my choosing.
UPDATE
I have tried setting the getMock
to some variable and then setting the return value. But PHPUnit doesn't seem to notice the variable and the return value I've set. This is because, I'm assuming, that the variable is never used anywhere. But with how the code is set up, there is no way to pass the mock object into foo so it knows to use that instead of the actual Bar class. I've added in the Foo and Bar class for easier reference. The original Bar class returns * but I want it to return ** from the mock class.
Using set_new_overload()
isn't mandatory for this test but so far using it is the only way I've been able to successfully mock Bar.
<?php
require_once 'Foo.php';
class FooTest extends PHPUnit_Framework_TestCase {
protected function setUp()
{
$mock = $this->getMock(
'Bar', /* name of class to mock */
array('doSomethingElse'), /* list of methods to mock */
array(), /* constructor arguments */
'BarMock' /* name for mocked class */
);
$mock->expects($this->any())
->method("doSomthingElse")
->will($this->returnValue("**")
);
set_new_overload(array($this, 'newCallback'));
}
protected function tearDown()
{
unset_new_overload();
}
protected function newCallback($className)
{
switch ($className) {
case 'Bar':
return 'BarMock';
default:
return $className;
}
}
public function testDoSomething()
{
$foo = new Foo;
$this->assertTrue($foo->doSomething());
}
}
?>
<?php
require_once 'Bar.php';
class Foo {
public function doSomething()
{
// ...
$bar = new Bar;
$bar->doSomethingElse();
// ...
return TRUE;
}
}
?>
<?php
class Bar {
public function doSomethingElse()
{
return '*';
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模拟类本身不会有任何预期的行为。要将其用作存根(调用方法 Y 时执行 X),您需要访问创建的 BarMock 实例,但看起来新的重载不会为您提供这一点。
相反,我会创建一个
Bar
的自定义子类,它从doSomethingElse()
返回您想要的内容。The mock class won't have any expected behavior by itself. To use it as a stub (do X when method Y is called) you need access to the
BarMock
instance created, but it doesn't look like the new overload will give you that.Instead I would create a custom subclass of
Bar
that returns what you want fromdoSomethingElse()
.我不太明白你在这里想做什么,但是:
I don't really understand what you're trying to do here, but: