5

I am writing a unit test in Laravel 5.0 and in my request class I am using a different bag to show the validation error messages.

I am using this in my 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'
        ];
    }
}

In my test file, I am testing using this:

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

If I run the tests, it can't get the right assert and return this problem:

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

If I take out the $errorBag attribute from the request file, I have no problems.

I can give more details as needed.

Whymarrh
  • 13,139
  • 14
  • 57
  • 108
giordanolima
  • 1,190
  • 1
  • 12
  • 21

2 Answers2

2

You can get an alternate bag from the session store like this:

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

However, Laravel does not use an alternate bag by default, so I'm assuming you're doing something in your code to register your App\Request::$errorBag with the session handler.

Ben Claar
  • 3,285
  • 18
  • 33
0

I don't know if you are setting your session elsewhere but I guess you may do something like:

$this->session(['foo' => 'bar']);

Before you can assert something in session. See testing helpers section for Laravel 5.0

jhmilan
  • 508
  • 3
  • 12