1

您好我正在尝试编写 PHPUnit 测试来测试我的登录控制器,并且我的代码似乎在浏览器中运行良好但是当我运行 php 单元测试时出现以下错误

错误:

Call to a member function with() on a non-object

方法:

public function store()
{
    $input = Input::only('email', 'password');
    $attempt = Auth::attempt($input);

    if($attempt){
        return Redirect::intended('/')
            ->with('flash_message', Lang::get('sessions.you_have_logged_in'));
    }
}

单元测试:

public function testStore()
{
    Auth::shouldReceive('attempt')
            ->once()
            ->andReturn(true);

    Redirect::shouldReceive('intended')
            ->once();

    $this->call('POST', 'session');
}

在使用嘲笑方面我有点菜鸟,所以我想问题可能在于我如何嘲笑重定向对象?

4

1 回答 1

0

你需要从 Redirect::shouldReceive(...) 返回一些东西

public function testStore()
{
    Auth::shouldReceive('attempt')
        ->once()
        ->andReturn(true);

    Redirect::shouldReceive('intended')
        ->once()
        ->andReturn($redirectResponse = Mockery::mock('Illuminate\Http\RedirectResponse'));

    $this->call('POST', 'session');
}

重定向正在正确接收“预期”,但是根据实际函数它​​返回一些东西(特别是 Illuminate\Http\RedirectResponse 的实例)

您可以查看 Illuminate\Routing\Redirector 以获得更多信息。

于 2014-02-28T19:59:25.053 回答