5

Sto scrivendo un test di unità in Laravel 5.0 e nella classe di richiesta sto utilizzando un altro sacchetto per mostrare i messaggi di errore di convalida.LARAVEL 5.0 + Unit Test - assertSessionHasErrors con diversi pacchetti

Sto usando questo nel mio file:

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

Nel mio file di prova, sto testando utilizzare questo:

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

Se corro i test, non può ottenere la giusta affermare e restituire questo problema:

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

Se prendo l'attributo $errorBag dal file di richiesta, non ho i problemi.

Posso fornire maggiori dettagli in base alle esigenze.

+0

Puoi spiegare perché hai 'protetto $ errorBag = 'otherbag';' nel tuo oggetto? Che cosa fa questo e perché? –

+0

Perché ho due posti per mostrare gli errori nella mia vista ... E lo gestisco con borse diverse .... – giordanolima

risposta

2

È possibile ottenere un sacchetto alternativo dal negozio di sessione in questo modo:

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

Tuttavia, laravel non usa un sacchetto alternativo di default, quindi sto supponendo che si sta facendo qualcosa nel codice per registra il tuo App\Request::$errorBag con il gestore di sessione.