5

我在Laravel 5.0,在我的请求类我使用的是不同的袋子来显示验证错误消息编写单元测试assertSessionHasErrors。LARAVEL 5.0 +单元测试 - 用不同的包装袋

我使用这在我的文件:

/* ExampleTest.php */ 
class ExampleTest extends TestCase { 
    public function testPostWithoutData(){ 
     $response = $this->call('POST', 'url/to/post',[ 
      'my_field' => '' 
     ]); 
     $this->assertSessionHasErrors('my_field'); 
    } 
} 

如果我运行测试,就不能得到正确的:

/* 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' 
     ]; 
    } 
} 

在我的测试文件,我使用的这个测试断言和返回这个问题:

Session missing error: my_field Failed asserting that false is true .

如果我拿出从请求文件中$errorBag属性,我没有问题。

我可以根据需要提供更多细节。

+0

你能解释一下为什么你的对象中有'protected $ errorBag ='otherbag';'?这是做什么的,为什么? –

+0

因为我有两个地方可以显示erros im我的看法...并且我处理它与不同的袋子.... – giordanolima

回答

2

您可以从会话存储这样获得的备用袋:

$myBag = $this->app('session_store')->getBag('otherBag'); 
$this->assertTrue($myBag->any()); 

然而,Laravel不使用默认的备用袋,所以我假设你正在做在你的代码的东西用会话处理程序注册您的App\Request::$errorBag