模拟类不接受Carbon的实例

时间:2015-01-02 17:20:18

标签: unit-testing laravel phpunit mockery

我正在尝试使用PhpUnit和Mockery测试方法。在指定方法的过程中应该使用参数调用我的测试失败。

TEST:

 $this->eventRepo = \Mockery::mock('Path\To\EventRepository');

 $start = Carbon::createFromFormat('Ymd-H-i-s', '20141211-09-21-00');
 $end = Carbon::createFromFormat('Ymd-H-i-s', '20141211-09-19-00');

 $this->eventRepo
        ->shouldReceive('findEvent')
        ->withArgs([
            $start,
            $end,
            '1',
            '1234567891'
        ])
        ->andReturn($callEvent);

真实代码:

    $start = Carbon::createFromFormat('Ymd-H-i-s', '20141211-09-20-00');
    $end = Carbon::createFromFormat('Ymd-H-i-s', '20141211-09-20-00');

    $event = $this->eventRepo->findEvent(
        $start->subSeconds(60),
        $end->addSeconds(60),
        $id,
        $number
    );

来自测试的错误:

 Mockery\Exception\NoMatchingExpectationException: No matching handler found for EventRepo::findEvent(object(Carbon\Carbon), object(Carbon\Carbon), "1", "1234567891"). Either the method was unexpected or its arguments matched no expected argument list for this method

$this->eventRepo在测试中被嘲笑。真正的代码运行正常。显示错误后,我猜var_dump()是Carbon的一个实例。

我不知道是什么原因引起的。我试着谷歌搜索它,但不知道谷歌做了什么,这是毫无价值的。有没有人遇到过这个?

1 个答案:

答案 0 :(得分:1)

with()withArgs()中使用对象时,phpunit会执行===检查。这意味着它将查找完全相同的类实例,而不仅仅是任何Carbon实例。

在这种情况下,这意味着findEvent()正在接收Carbon的实例,但不是实际代码中的实例。

相关问题