【发布时间】:2015-10-09 23:09:22
【问题描述】:
我正在 Laravel 5.0 中编写单元测试,在我的请求类中我使用不同的包来显示验证错误消息。
我在我的文件中使用这个:
/* ExampleRequest.php */
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Support\Facades\Auth;
class ExampleRequest extends Request {
protected $errorBag = 'otherbag';
public function rules(){
return [
'my_field' => 'required'
];
}
}
在我的测试文件中,我正在使用这个进行测试:
/* ExampleTest.php */
class ExampleTest extends TestCase {
public function testPostWithoutData(){
$response = $this->call('POST', 'url/to/post',[
'my_field' => ''
]);
$this->assertSessionHasErrors('my_field');
}
}
如果我运行测试,它无法获得正确的断言并返回此问题:
会话丢失错误:
my_field断言false是true失败。
如果我从请求文件中取出$errorBag属性,就没有问题了。
我可以根据需要提供更多详细信息。
【问题讨论】:
-
你能解释一下为什么你的对象中有
protected $errorBag = 'otherbag';吗?这是做什么的,为什么? -
因为我有两个地方可以展示我的观点......而且我用不同的袋子处理它......
标签: php unit-testing laravel-5 laravel-validation laravel-request