Mockery mock没有返回指定的值

时间:2015-04-15 11:15:11

标签: unit-testing laravel mockery

我在Laravel项目中使用Mockery来模拟User Eloquent模型并测试路径。

这是我测试/api/user/activate路线的方式:

<?php

use Illuminate\Support\Facades\Session;

class ActivateTest extends TestCase
{
    private $userMock;

    public function setUp()
    {
        parent::setUp();

        $this->userMock = Mockery::mock('App\User');

        Session::start();
    }

    public function tearDown()
    {
        Mockery::close();
    }

    public function testActivate()
    {
        $this->userMock->shouldReceive('where->first')->once()->andReturn('test');
        $this->userMock->shouldReceive('activate')->once();

        $response = $this->call('POST', '/api/user/activate', [
            'activationToken' => '838jfjnvu83u3',
            '_token' => csrf_token()
        ]);

        // This will be displayed in the PHPunit output
        print_r($response->getContent());

        $this->assertResponseStatus(200);
    }
}

我遇到的问题是andReturn('test')似乎不起作用。 PHPunit结果是:

F{"error":{"message":null,"statusCode":404}}

Time: 276 ms, Memory: 15.50Mb

There was 1 failure:

1) ActivateTest::testActivate
Failed asserting that 404 matches expected 200.

这是activate()中的UserController的内容:

public function activate(Request $request)
{
    $activation = $request->input();

    $user = $this->user->where('activationToken', $activation['activationToken'])->first();

    if(!$user) return $this->respondNotFound($user);

    try
    {
        $user->activate($activation['password']);
    }
    catch(ModelException $e)
    {
        return $this->respondInternalError($e->errorMessages());
    };

    return $this->respondCreated('Account activated.');
}

问题是控制器中的$usernull,因为模拟不返回test(在这种情况下,条件将评估为true,我不会得到404响应)。

修改

我也尝试过使用PHPunit模拟但是没有成功:

$this->userMock = $this->getMockBuilder('App\User')->setMethods(['where', 'first', 'activate'])->getMock();

$this->userMock->expects($this->once())->method('where')->willReturn($this->userMock);
$this->userMock->expects($this->once())->method('first')->willReturn('test');
$this->userMock->expects($this->once())->method('activate');

2 个答案:

答案 0 :(得分:0)

模拟对象是不够的。您需要将该模拟对象注入包含activate()函数的类。

您也可以在setUp()功能中执行此操作。尝试添加此...

$this->app->instance('App/User', $this->userMock);

这将告诉Laravel何时想要注入App/User的实例,以注入刚刚创建的模拟对象。

答案 1 :(得分:0)

问题是由->first()引起的,因为它不是EloquentUser类中既不存在的方法。

为了解决这个问题,我创建了一个新的UserRepository并将其作为依赖项注入控制器构造函数。

class UserRepository implements UserRepositoryInterface
{
    /**
     * @var User
     */
    protected $user;

    /**
     * @param User $user
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * @param $activationToken
     * @return mixed
     */
    public function whereActivationToken($activationToken)
    {
        return $this->user->where('activationToken', $activationToken)->first();
    }
}

注入UserController

public function __construct(UserRepository $userRepository)
{
    $this->userRepository = $userRepository;
}

这就是测试PostActivateTest类现在的样子:

use Illuminate\Support\Facades\Session;

class PostActivateTest extends TestCase
{
    private $user;
    private $userRepositoryMock;

    public function setUp()
    {
        parent::setUp();

        $this->user               = Mockery::mock('App\User');
        $this->userRepositoryMock = Mockery::mock('Repository\Database\UserRepository');
        $this->app->instance('App\User', $this->user);
        $this->app->instance('Bloom\BloomCRM\Repository\Database\UserRepository', $this->userRepositoryMock);

        Session::start();
    }

    public function tearDown()
    {
        Mockery::close();
    }

    public function testActivate()
    {
        $this->userRepositoryMock->shouldReceive('whereActivationToken')->once()->andReturn($this->user);
        $this->user->shouldReceive('activate')->once();

        $this->call('POST', '/api/user/activate', [
            'activationToken' => '838jfjnvu83u3',
            'password' => 'test',
            '_token' => csrf_token()
        ]);

        $this->assertResponseStatus(201);
    }
}