1

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

我想避免执行此操作:

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

我试过了:

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

我得到了错误:

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

我发现了当调用有参数时如何适应嘲弄,但没有发现如何避免这个问题。

谁能帮我?

4

2 回答 2

1

我找到了一个解决方案,但如果有人知道,我仍然想知道如何用嘲讽来做到这一点。

我这样做时的错误:

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

曾是:

InvalidArgumentException: Cannot redirect to an empty URL.

这就是为什么我试图用嘲弄来避免 Redirect::back 调用,但你可以避免这样做:

$this->call('POST', 'password-reminder', $data, array(), array('HTTP_REFERER' => '/password-reminder'));

实际上,您在 HTTP_REFERER 上编写的 url 对于测试并不重要。

查找:http ://alexsears.com/article/testing-redirectback-laravel

于 2014-03-14T12:11:17.937 回答
0

您的第一个测试应该进行一个小的调整。

 $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);

您已经模拟了请求外观以正确接收,但默认情况下模拟返回 null ,除非使用 shouldReturn 方法另有指示。

于 2014-05-11T13:45:02.733 回答