【问题标题】:Avoid Redirect::back with mockery避免 Redirect::back 嘲讽
【发布时间】:2014-04-19 16:48:59
【问题描述】:

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

我想避免这个被执行:

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

我试过了:

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

我得到了错误:

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

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

谁能帮帮我?

【问题讨论】:

    标签: php unit-testing laravel-4 mockery


    【解决方案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

    【讨论】:

      【解决方案2】:

      您的第一个测试应该稍作调整。

       $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-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-20
        • 1970-01-01
        • 2016-02-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多