Php 路由重定向中的laravel测试失败
我正在测试一个表单。成功后,它必须重定向到路由 这是路线摘录Php 路由重定向中的laravel测试失败,php,forms,testing,laravel,controller,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'=>'getcatego
<?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'));
});
这里是风景
@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
测试失败了。这就是我收到的错误:
There was 1 failure:
1) CategoryControllerTest::testPassedPostCategoryForm
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/categories'
+'http://localhost/categories/addcategory'
在您的代码中,没有传递验证程序。 因此重定向回
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->call('POST', 'categories/addcategory');
[编辑]进一步,删除以下内容
$this->mock
->shouldReceive('create')
->once();
,因为没有在控制器中调用“create”方法。我替换了
$this->call('POST','categories/addcategory')代码>带有$this->route('POST','getcategoryform',数组('category_name'=>'dummycat','options'=>'dummyoption')代码>。现在它显示了PHP致命错误:在第128行的/var/www/hututo/vendor/mockry/mockry/library/mockry.PHP中对非对象调用成员函数fetchMock()
这是另一个问题。在控制器中未调用create
方法。我补充我的答案。
$this->route('POST','getcategoryform',array( 'category_name'=> 'dummycat' ,
'options' => 'dummyoption'));
$this->call('POST', 'categories/addcategory');
$this->mock
->shouldReceive('create')
->once();