Support the ongoing development of Laravel.io →
Forms Validation Testing

i am testing a form. Upon success it must redirect to a route and upon failure it must return to the same route. But test is failing everytime.

Here is the route excerpt

<?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'));
});

This is the controller


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);
    }

here's the view

@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

This is the test that i tried:

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');


    }

The test is failing everytime. This is the error i'm, receiving:

There was 1 failure:

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

What are the errors that $validator is throwing?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.