lundi 25 avril 2016

Laravel 5 - Unit testing - status code 500, expected 200

Why unit test in "test 1" returns me status code 500, not 200 ? Can somebody explain me ? Here is example in 2 tests for same action and they return different status code. I expected 200 in both tests ?

LanguageController

    class LanguageController extends Controller implements IEntityViewManager
    { 
          public function showAllView()
          {
              $allLanguages = $this->languageRepo->orderBy('id');

              return view('admin.languages.showAll')->with('languages', $allLanguages);
          }
    }

LanguageControllerTest

class LanguageControllerTest extends TestCase
{

    public function __construct($name = NULL, array $data = array(), $dataName = '')
    {
        parent::__construct($name, $data, $dataName);
    }

    public function setUp()
    {
        parent::setUp();
    }

    public function tearDown()
    {
        Mockery::close();
    }

    protected function setUpMock()
    {
        $mock = Mockery::mock(LanguageRepositoryInterface::class);
        $this->app->instance(LanguageRepositoryInterface::class, $mock);

        return $mock;
    }

    // test 1
    public function testShowAllLanguages()
    {
        $mock = $this->setUpMock();

        $mock->shouldReceive('orderBy')->once()->andReturn([1]);

        $result = $this->action('GET', 'Entities\LanguageController@showAllView');

        var_dump("Test 1 : " . $result->getStatusCode()); // RETURNS 500
    }

    // test 2
    public function testShowAllView()
    {
        $result = $this->action('GET', 'Entities\LanguageController@showAllView');

        var_dump("Test 2 : " . $result->getStatusCode()); // RETURNS 200

        $this->assertViewHas('languages');

        $this->assertResponseOk();
    }
}

Responses in cmd:

enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire