Laravel 4.2:使用assertRedirectedToRoute进行测试失败

时间:2015-01-18 11:25:07

标签: php laravel laravel-4 laravel-testing

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

当我按照书中的例子时 - 我收到了消息:

  

断言Illuminate \ Http \ Response Object(...)是一个失败   类的实例" 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);
    }

当Redirect Facade解析为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这是假设要照顾所有环境设置等等。任何想法?

1 个答案:

答案 0 :(得分:1)

发表评论作为回答:

由于您正在使用资源丰富的对象,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 |
+-----------------------------+---------------+-------------------------+

正如您所看到的,对POST / POST(在测试中执行)的POST请求会触发PostsController上的store()方法,而不是您认为正在测试的create()方法

createstore以及editupdate有时会让人感到困惑。这是区别:

  • create() - 显示创建新资源的表单
  • store() - 实际上是根据发布的数据创建资源

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