我正在 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
属性,我没有问题。
我可以根据需要提供更多详细信息。