使用andReturn返回新对象的Mockery

时间:2016-03-08 11:27:39

标签: php unit-testing laravel mocking phpunit

我有以下方法:

public function getThreads()
{
    $results = \DB::select(\DB::raw(config('sql.messages.threads')), [$this->user->user_id]);

    $messagesThreadsModel = app()->make(MessagesThreadsModel::class);
    $messages = [];

    foreach ($results as $r) {
        $message = $messagesThreadsModel->find($r->messages_thread_id);
        $message->unread = $r->unread;
        $messages[] = $message;
    }

    return $messages;
}

为了测试上面的内容,我模拟了调用\ DB :: select(通过Laravel'的外观)来返回对象列表,就像DB类通常那样。然后我加载消息线程模型,再次在容器中进行模拟和替换(因此app() - > make()将返回其模拟实例,而不是实际模型)。

最后:

$messagesThreadsModel->find($r->messages_thread_id);

再次模拟返回虚拟对象(存根?)。全部内容如下:

$threadsList = $this->mockThreads();

    // mock the raw expression, check the query 
    \DB::shouldReceive('raw')->once()->with(m::on(function($sql)
    {
        return strpos($sql, 'messages') !== false;
    }))->andReturn(true);

    // mock the DB call, return a list of objects
    \DB::shouldReceive('select')->once()->with(true, [$this->usersModel->user_id, $this->usersModel->user_id, $this->usersModel->user_id])->andReturn($threadsList);

    $mockThreadResult = new \StdClass;
    $mockThreadResult->last = "date";

    $this->messagesThreadModel->makePartial();

    // HERE IS THE TRICKY PART!
    $this->messagesThreadModel->shouldReceive('find')->times(count($threadsList))->andReturn($mockThreadResult);

    $this->app->instance('App\Freemiom\Models\Messages\MessagesThreads', $this->messagesThreadModel);

    $messages = new Messages($this->usersModel);
    $threadList = $messages->getThreads();

现在问题是什么?因为我传递了已创建的虚拟对象,所以每次在循环中调用 - > find方法时都会返回相同的对象。

我应该如何通过每次调用告诉mockery返回一个新对象?它甚至可能吗?或许我应该做一些代码重构以使其全部可测试?

1 个答案:

答案 0 :(得分:2)

因此,为了能够在每次连续调用相同的模拟方法时返回一个新对象,我必须像这样使用 andReturnUsing

$this->messagesThreadModel->shouldReceive('find')->times(count($threadsList))->andReturnUsing(function()
{
    $mockThreadResult = new \StdClass;
    $mockThreadResult->last = "date";

    return $mockThreadResult;
});

这将模仿Eloquent模型的行为,该模型也使用find()方法返回新对象。