Laravel 4.2 : 使用断言重定向到路由进行测试失败


Laravel 4.2 : testing with assertRedirectedToRoute fails

我想知道是否有其他人遇到过这个问题。我正在阅读Jeffrey Way关于Laravel测试的书,我正在解释如何测试控制器的章节。

当我按照书中的例子进行操作时 - 我得到的消息:

断言 Illuminate''Http''Response 对象 (...) 是 类 "Illuminate''Http''RedirectResponse" 的实例。

我的测试如下:

public function testStoreFails()
    {
        $input = ['title' => ''];
        $this->mock
                ->shouldReceive('create')
                ->once()
                ->with($input);
        $this->app->instance('Post', $this->mock);
        $this->post('posts', $input);
        $this->assertRedirectedToRoute('posts.create');
        $this->assertSessionHasErrors(['title']);
    }

以及控制器中非常简单的方法(只是为了测试这个特定场景):

public function create()
    {
        $input = Input::all();
        $validator = Validator::make($input, ['title' => 'required']);
        if ($validator->fails()) {
            return Redirect::route('posts.create')
                    ->withInput()
                    ->withErrors($validator->messages());
        }
        $this->post->create($input);
        return Redirect::route('posts.index')
                ->with('flash', 'Your post has been created!');
    }

从我所看到的AssertionsTrait::assertRedirectedTo

检查例如Illuminate'Http'RedirectResponse
/**
     * Assert whether the client was redirected to a given URI.
     *
     * @param  string  $uri
     * @param  array   $with
     * @return void
     */
    public function assertRedirectedTo($uri, $with = array())
    {
        $response = $this->client->getResponse();
        $this->assertInstanceOf('Illuminate'Http'RedirectResponse', $response);
        $this->assertEquals($this->app['url']->to($uri), $response->headers->get('Location'));
        $this->assertSessionHasAll($with);
    }
    /**
     * Assert whether the client was redirected to a given route.
     *
     * @param  string  $name
     * @param  array   $parameters
     * @param  array   $with
     * @return void
     */
    public function assertRedirectedToRoute($name, $parameters = array(), $with = array())
    {
        $this->assertRedirectedTo($this->app['url']->route($name, $parameters), $with);
    }

当重定向外观解析为Illuminate'Routing'Redirector并且其route()方法调用createRedirect()时,它应该可以正常工作,它返回Illuminate'Http'RedirectResponse的实例 - 所以不太确定是什么原因造成的。

更新:

刚刚再次检查了代码,看起来问题出在AssertionsTrait::assertRedirectedTo()方法中。对 $this->client->getResponse() 的调用返回 Illuminate'Http'Response 的实例而不是Illuminate'Http'RedirectResponse - 因此$this->assertInstanceOf('Illuminate'Http'RedirectResponse', $response)调用失败。但我仍然不确定为什么 - 我正在扩展应该处理所有环境设置等的TestCase。 知道吗?

发布评论作为答案:

由于您正在使用资源丰富的对象,Laravel会自动为您创建一些路由,即:

+-----------------------------+---------------+-------------------------+
| URI                         | Name          | Action                  |
+-----------------------------+---------------+-------------------------+
| GET|HEAD posts              | posts.index   | PostsController@index   |
| GET|HEAD posts/create       | posts.create  | PostsController@create  |
| POST posts                  | posts.store   | PostsController@store   |
| GET|HEAD posts/{posts}      | posts.show    | PostsController@show    |
| GET|HEAD posts/{posts}/edit | posts.edit    | PostsController@edit    |
| PUT posts/{posts}           | posts.update  | PostsController@update  |
| PATCH posts/{posts}         |               | PostsController@update  |
| DELETE posts/{posts}        | posts.destroy | PostsController@destroy |
+-----------------------------+---------------+-------------------------+

如您所见,对/posts 的 POST 请求(就像您在测试中所做的那样)触发了 PostsController 上的 store() 方法,而不是您假设正在测试的create()方法。

createstore以及editupdate有时会令人困惑。区别如下:

  • create() - 显示用于创建新资源的窗体
  • store() - 实际从发布的数据创建资源

  • edit($id) - 显示用于编辑指定资源的表单
  • update($id) - 实际使用发布的数据更新资源