Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LARAVEL 5.0 + Unit Test - assertSessionHasErrors with different bags

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.

like image 211
giordanolima Avatar asked Oct 30 '22 23:10

giordanolima


1 Answers

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.

like image 78
Ben Claar Avatar answered Nov 08 '22 13:11

Ben Claar