laravel测试在路由重定向中失败

时间:2014-05-08 07:47:53

标签: php forms testing laravel controller

我正在测试一张表格。成功后,它必须重定向到路线。

这是路线摘录

<?php

Route::group(array('prefix'=>'categories'), function(){
    Route::get('/', array('as'=>'categories', 'uses'=>'CategoryController@getCategory'));
    Route::get('addcategory',array('as'=>'getcategoryform', 'uses'=>'CategoryController@getCategoryForm'));
    Route::post('addcategory', array('as'=>'postcategoryform', 'uses' => 'CategoryController@postCategoryForm'));
});

这是控制器

class CategoryController extends BaseController {

   // adding Category model instance in the controller through the constructor
    public function __construct(Category  $category){
        $this->category = $category;
    }

    public function getCategoryForm(){
        return View::make('dashboard.addcategoryform');
    }

    public function postCategoryForm(){
        $rules = ['category_name' => 'required|between:3,20', 'options' =>'required'];
        $validator = Validator::make(Input::all(), $rules);

        if($validator->passes()){
            $category = new Category;
            $category->category_name = Input::get('category_name');
            $category->options = Input::get('options');
            $category->save();
            Session::flash('message', 'Category Added Successfully');

            return Redirect::route('categories');
        }else return Redirect::route('getcategoryform')->withErrors($validator);
    }

这里是视图

@extends('layouts.main')

@section('content')
    <div class="g12">
        <h1>Add Category</h1>

        {{Form::open(array('route'=>'postcategoryform'))}}
            {{ Form::text('category_name', Input::old('category_name'), array('placeholder' => 'eg article') )}}

            <textarea id="textarea_auto" name="options" value="Input::old('options')" placeholder="eg. author, facts, tags, reference, book"></textarea>

            {{Form::submit('Add')}}
        {{Form::close()}}
    </div>
@stop

这是我试过的测试:

public function testPassedPostCategoryForm(){
        Input::replace(['category_name' => 'dummycat', 'options' => 'dummyoption']);

        $this->mock
            ->shouldReceive('create')
            ->once();

        $this->app->instance('Category', $this->mock);

        $this->call('POST', 'categories/addcategory');

        $this->assertRedirectedToRoute('categories');


    }

测试失败。这是我收到的错误:

There was 1 failure:

1) CategoryControllerTest::testPassedPostCategoryForm
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/categories'
+'http://localhost/categories/addcategory'

1 个答案:

答案 0 :(得分:0)

在您的代码中,验证程序未通过。 因此,重定向回http://localhost/categories会导致失败结果。

如果你删除验证器,那就顺利了,只需返回

public function postCategoryForm(){
    $rules = ['category_name' => 'required|between:3,20', 'options' =>'required'];
    $validator = Validator::make(Input::all(), $rules);

    return Redirect::route('categories');
}

或者您可以按如下方式重写测试代码,

$this->call('POST', 'categories/addcategory', array( 'category_name'=> 'dummycat' ,
             'options' => 'dummyoption'));

$this->route('POST','getcategoryform',array( 'category_name'=> 'dummycat' ,
             'options' => 'dummyoption'));

而不是

$this->call('POST', 'categories/addcategory');

[编辑]此外,删除以下内容

$this->mock
->shouldReceive('create')
->once();

,因为&#39;创造&#39;方法未在控制器中调用。