Laravel 4.1 PHPUnit测试:在非对象上使用()调用成员函数

时间:2014-01-29 14:01:29

标签: php unit-testing laravel-4 phpunit mockery

您好我正在尝试编写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');
}

当涉及到使用嘲弄时,我有点像菜鸟,所以我猜问题可能在于我如何模仿重定向对象?

1 个答案:

答案 0 :(得分: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以获得进一步的见解。