【发布时间】:2021-09-03 07:43:35
【问题描述】:
考虑以下类重现我的真实用例:
namespace App;
class Foo
{
public function greeting(): string
{
return (new Greeting())->welcome();
}
}
namespace App;
class Greeting
{
public function welcome(): string
{
return 'hello world';
}
}
考虑到这个使用Mockery(版本1.3.4)的测试类重载Foo类(new Greeting())中的hard dependency:
namespace Test;
use App\Foo;
use App\Greeting;
use Mockery;
use PHPUnit\Framework\TestCase;
class FooTest extends TestCase
{
public function testGreeting(): void
{
$greetingMock = Mockery::mock('overload:' . Greeting::class);
$greetingMock->shouldReceive('get')
->twice()
->andReturn(
'foo',
'bar'
);
$foo = new Foo();
echo $foo->greeting() . PHP_EOL;
echo $foo->greeting() . PHP_EOL;
}
}
我想知道为什么输出是:
foo
foo
而不是:
foo
bar
我是否误读了有关 andReturn 方法的文档?:
可以为多个返回值设置期望。通过提供一系列返回值,我们告诉 Mockery 在每次后续调用该方法时要返回什么值
【问题讨论】:
标签: php unit-testing phpunit mockery