mercredi 1 juin 2016

Laravel 5.0 Unit Testing problems with Session variables

I am trying to do some unit testing on some existing code. My controller looks something like

class DefaultController extends Controller
{
    public function index() {
        if (!Session::get('answers', [])) {
            App::abort(403, 'Error.');
        }

        // Do rest of the stuff here
    }
}

and my test class looks something like

class DefaultController extends extends TestCase {
    public function testIndex_withoutSession() {
        // Arrange
        /* Nothing to arrange now */

        // Act
        $this->action('GET', 'DefaultController@index');

        // Assert
        $this->assertResponseStatus(403);
    }

    public function testIndex_withSession() {
        // Arrange
        $this->session(['answers' => array()]);

        // Act
        $this->action('GET', 'ParticipantController@create');

        $this->assertSessionHas('answer');
        // this function is giving true

        // Assert
        $this->assertResponseStatus(200);

        $this->flushSession();
    }
}

My test cases without the session is working fine but when I want to check it by mocking the session variable 'answers' it is still giving me the error. Can anyone please help me out by figuring what am I doing wrong or how can I do it properly? Without this I cannot proceed any further in checking the code. Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire