【发布时间】:2012-09-26 03:06:04
【问题描述】:
我一定遗漏了一些东西,但我遵循了本教程:http://www.phpunit.de/manual/current/en/test-doubles.html
<?php
class SomeClass
{
public function doSomething()
{
// Do something.
return 'bar';
}
}
?>
我的 StubTest 课程
class StubTest extends PHPUnit_Framework_TestCase
{
public function testStub()
{
// Create a stub for the SomeClass class.
$stub = $this->getMock('SomeClass');
// Configure the stub.
$stub->expects($this->any())
->method('doSomething')
->will($this->returnValue('foo'));
// Calling $stub->doSomething() will now return
$this->assertEquals('foo', $stub->doSomething());
}
}
?>
好吧,也许我遗漏了一些东西,但这不是调用 doSomething 的预期值吗?
如果我这样做$this->assertEquals('bar', $stub->doSomething());,它将失败。
它似乎是针对->will($this->returnValue('foo'));的基础
【问题讨论】:
-
您的代码似乎没问题。你是什么意思“它失败了”? $stub->doSomething() 返回 'bar' 或者它可能返回 null 或测试崩溃?你用的是哪个版本的phpunit?
标签: php class object mocking phpunit