Php 避免重定向::用嘲弄返回

Php 避免重定向::用嘲弄返回,php,unit-testing,laravel-4,mockery,Php,Unit Testing,Laravel 4,Mockery,我只是想写一些关于Laravel 4密码提醒的测试: 我希望避免执行此操作: case Password::INVALID_USER: return Redirect::back()->with('error', Lang::get($response)); 我试着: Redirect::shouldReceive('back')->once(); 我得到了一个错误: PHP致命错误:在非对象上使用()调用成员函数 我发现了当调用具有参数时如何调整模拟,而不是如何避免这个

我只是想写一些关于Laravel 4密码提醒的测试:

我希望避免执行此操作:

case Password::INVALID_USER:
    return Redirect::back()->with('error', Lang::get($response));
我试着:

Redirect::shouldReceive('back')->once();
我得到了一个错误:

PHP致命错误:在非对象上使用()调用成员函数

我发现了当调用具有参数时如何调整模拟,而不是如何避免这个问题


有人能帮我吗?

我找到了一个解决办法,但如果有人知道的话,我仍然想知道如何通过嘲弄来解决这个问题

执行以下操作时出现的错误:

$this->call('POST', 'password-reminder', $data)
是:

这就是为什么我试图通过模拟来避免重定向::回叫,但您可以通过这样做来避免它:

$this->call('POST', 'password-reminder', $data, array(), array('HTTP_REFERER' => '/password-reminder'));
实际上,您在HTTP\U REFERER上编写的url对于测试来说并不重要


查找:

您的第一次测试只需进行一次小的调整即可工作

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

 //you could do additional assertions on your $fakeRedir here
 // $fakeRedir->shouldReceive('with')->once()->with(/* argument asserttions here*/);
 $result = $yourInstance->yourMethodUnderTest();
 $this->assertEquals($fakeRedir,$result);
您已经模拟了请求facade以正确接收,但是默认情况下,除非在shouldReturn方法中另有指示,否则模拟将返回null

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

 //you could do additional assertions on your $fakeRedir here
 // $fakeRedir->shouldReceive('with')->once()->with(/* argument asserttions here*/);
 $result = $yourInstance->yourMethodUnderTest();
 $this->assertEquals($fakeRedir,$result);