2

我如何在 laravel 4 中用参数模拟外观?例如,我正在尝试测试我的用户控制器和我的“登录”方法。

我的控制器方法

public function login(){

    $this->beforeFilter('guest');

    $creds = array(
        'email'    => Input::get('email'),
        'password' => Input::get('password'),
    );

    if(Auth::attempt($creds, true)){
        return "successful";
    } else {
        return Redirect::to('user/login')->with('error', true);
    }
}

不起作用的重定向测试

    public function testPostLogin(){

        Redirect::shouldReceive('to')->once()->with('error', true);

        $response = $this->action('POST', 'UserController@login');

        $this->assertRedirectedTo('user/login');

    }

我收到以下异常。我不知道如何将“用户/登录”参数注入重定向模拟

Mockery\Exception\NoMatchingExpectationException : 没有为 Illuminate\Routing\Redirector::to("user/login") 找到匹配的处理程序

4

1 回答 1

10

从理论上讲,您可以模拟您的Auth课程。

尝试这个:

Auth::shouldReceive('attempt')->once()->andReturn(true);
于 2013-05-21T20:04:51.747 回答