3

我正在尝试测试控制器操作。该操作应调用模型上的函数,返回模型。在测试中,我模拟了模型,将其绑定到 IoC 容器。我通过其构造函数将依赖项注入到控制器中。然而不知何故,没有找到并调用模拟,而是调用了模型的实时版本。(我可以说,因为正在生成日志。)

首先,我的单元测试。创建模拟,告诉它期待一个函数,将它添加到 IoC 容器,调用路由。

public function testHash(){
    $hash = Mockery::mock('HashLogin');
    $hash->shouldReceive('checkHash')->once();

    $this->app->instance('HashLogin', $hash);

    $this->call('GET', 'login/hash/c3e144adfe8133343b37d0d95f987d87b2d87a24');
}

其次,注入依赖项的我的控制器构造函数。

public function __construct(User $user, HashLogin $hashlogin){
    $this->user = $user;
    $this->hashlogin = $hashlogin;
    $this->ip_direct = array_key_exists("REMOTE_ADDR",$_SERVER) ? $_SERVER["REMOTE_ADDR"] : null;
    $this->ip_elb = array_key_exists("HTTP_X_FORWARDED_FOR",$_SERVER) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : null;

    $this->beforeFilter(function()
    {
        if(Auth::check()){return Redirect::to('/');}
    });
}

然后是我的控制器方法。

public function getHash($code){
    $hash = $this->hashlogin->checkHash($code);
    if(!$hash){
        return $this->badLogin('Invalid Login');
    }
    $user = $this->user->getFromLegacy($hash->getLegacyUser());
    $hash->cleanup();
    $this->login($user);
    return Redirect::intended('/');
}

控制器方法被正确调用,但它似乎没有看到我的 Mock,所以它正在调用实际模型的函数。这导致模拟的期望失败,并导致对数据库的检查是不可取的。

我在另一个测试中也遇到了同样的问题,尽管这个测试使用了 Laravel 内置的 Facades。

考试:

public function testLoginSuccessfulWithAuthTrue(){
    Input::shouldReceive('get')->with('username')->once()->andReturn('user');
    Input::shouldReceive('get')->with('password')->once()->andReturn('1234');
    Auth::shouldReceive('attempt')->once()->andReturn(true);
    $user = Mockery::mock('User');
    $user->shouldReceive('buildRBAC')->once();
    Auth::shouldReceive('user')->once()->andReturn($user);

    $this->call('POST', 'login');

    $this->assertRedirectedToRoute('index');
}

控制器方法:

public function postIndex(){
    $username = Input::get("username");
    $pass = Input::get('password');
    if(Auth::attempt(array('username' => $username, 'password' => $pass))){
        Auth::user()->buildRBAC();
    }else{
        $user = $this->user->checkForLegacyUser($username);
        if($user){
            $this->login($user);
        }else{
            return Redirect::back()->withInput()->with('error', "Invalid credentials.");
        }
    }
    return Redirect::intended('/');
}

我收到错误:

Mockery\Exception\InvalidCountException: Method get("username") from Mockery_5_Illuminate_Http_Request should be called exactly 1 times but called 0 times."

同样,我知道该方法被正确调用,只是似乎没有使用模拟。

4

1 回答 1

4

解决了。我之前曾尝试在一个地方或另一个地方使用命名空间,但显然Mockery::mock, 和app->instance()需要完全命名空间的名称。我在其他测试中没有出现这个问题,所以我什至没有考虑过。我希望这对其他人有所帮助,因为这个让我绞尽脑汁了一段时间。

相关代码已修复:

$hash = Mockery::mock('App\Models\Eloquent\HashLogin');
$this->app->instance('App\Models\Eloquent\HashLogin', $hash);
于 2014-04-24T15:32:25.510 回答