Laravel unittest失败,没有为Auth :: attempt找到匹配的处理程序

时间:2014-12-29 17:00:49

标签: laravel-4 phpunit mockery

我似乎遇到了有条件的登录测试问题。我的登录处理程序检查电子邮件和密码是否有效以及正在确认的用户。以下测试用例通过:

public function test_redirect_to_login_if_login_fails()
{
    // enable filters to test login
    Route::enableFilters();
    // some fake credentials to use
    $credentials = [
        'email'=>'user@domain.com',
        'password'=>'badpassword',
        'confirmed'=>1
    ];
    // mock the Auth class
    Auth::shouldReceive('attempt')
        ->once()
        ->with($credentials)
        ->andReturn(false);
    // call the login post with the credentials
    $this->call('POST','login',$credentials);
    // assert that we are redirected back to the entry page
    $this->assertRedirectedToRoute('entry');
}

public function test_redirect_if_login_success()
    {
        // enable filtere to test login
        Route::enableFilters();
        // some fake credentials to use
        $credentials = [
            'email'=>'user@domain.com',
            'password'=>'Pa55w0rd',
            'confirmed'=>1
        ];
        // mock the Auth class
        Auth::shouldReceive('attempt')
            ->once()
            ->with($credentials)
            ->andReturn(true);
        // call the login post with the proper credentials
        $response = $this->call('POST','login',$credentials);
        // assert the response is good
        $this->assertTrue($response->isRedirection());
    }

然而,这个给了我一个错误 - 尽管它与上面的第一个测试用例非常相似:

public function test_redirect_if_user_is_not_confirmed()
    {
        // enable filters to test login
        Route::enableFilters();
        // some fake credentials to use
        $credentials = [
            'email'=>'user@domain.com',
            'password'=>'badpassword',
            'confirmed'=>0
        ];

        // mock the Auth class
        Auth::shouldReceive('attempt')
            ->once()
            ->with($credentials)
            ->andReturn(false);
        // call the login post with the credentials
        $this->call('POST','login',$credentials);
        // assert that we are redirected back to the entry page
        $this->assertRedirectedToRoute('entry');
    }

错误:

  

Mockery \ Exception \ NoMatchingExpecationException:没有匹配的处理程序   找到了   Mockery_0_Illumnate_Auth_AuthManager ::尝试(阵列('电子邮件' = GT;' user@domain.com','密码' = GT;' badpassword' '确认&#39 =大于1,))。   方法是意外的,或者它的参数不符合预期   此方法的参数列表。

我的控制器方法:

public function store()
{
    if (Auth::attempt(array("email"=>Input::get('email'),"password"=>Input::get('password'),'confirmed'=>1)))
    {
        return Redirect::intended('home');
    }
    return Redirect::route("entry")->with("danger",Lang::get('orientation.invalid'))->withInput();
}

1 个答案:

答案 0 :(得分:0)

您的测试失败正确 - 因为您向测试提供了错误的数据。

你有模拟Auth接收:

Auth::shouldReceive('attempt')
        ->once()
        ->with($credentials)
        ->andReturn(false);

特别是应该收到->with($credentials)。但是,当您的控制器始终发送$credentials时,您已将confirmed => 0定义为confirmed => 1,因为这是硬编码的。

相反 - 你应该期待在你的测试中接收confirmed => 1 - 但是因为找不到匹配的记录而嘲笑false返回。

这应该有效:

// some fake credentials to use
        $credentials = [
            'email'=>'user@domain.com',
            'password'=>'badpassword',
            'confirmed'=>1
        ];