【问题标题】:laravel test fail in route redirectionlaravel 测试在路由重定向中失败
【发布时间】:2014-05-08 07:47:52
【问题描述】:

我正在测试一个表单。成功后它必须重定向到一个路由。

这是路线摘录

<?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'

【问题讨论】:

    标签: php forms testing laravel controller


    【解决方案1】:

    在您的代码中,验证器未通过。 所以重定向回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();
    

    ,因为控制器中没有调用'create'方法。

    【讨论】:

    • 我将 $this-&gt;call('POST', 'categories/addcategory'); 替换为 $this-&gt;route('POST','getcategoryform',array('category_name' =&gt; 'dummycat' , 'options' =&gt; 'dummyoption') ); 。现在它显示PHP Fatal error: Call to a member function fetchMock() on a non-object in /var/www/Hututoo/vendor/mockery/mockery/library/Mockery.php on line 128
    • 这是另一个问题。 create 方法未在您的控制器中调用。我添加我的答案。
    猜你喜欢
    • 2017-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 2012-02-12
    相关资源
    最近更新 更多