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

眉间皱痕 提交于 2019-12-12 13:32:38

问题


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:


回答1:


I checked laravel.log and I found next logs:

[2016-04-26 08:45:49] testing.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object' in C:\xampp\htdocs\STP\storage\framework\views\76c117f88e2ab8d2c5f85f5187e254573559a2c3.php:7 Stack trace:

and next log:

Next exception 'ErrorException' with message 'Trying to get property of non-object (View: C:\xampp\htdocs\STP\resources\views\admin\languages\showAll.blade.php)' in C:\xampp\htdocs\STP\storage\framework\views\76c117f88e2ab8d2c5f85f5187e254573559a2c3.php:7

Stack trace:

and on my view i access to $language properties with :

$languages->char, $language->name

but it is array so I should access with:

$language['char'], $language['name']

and both tests now work properly and returns status code 200.

Thanks all for help.



来源:https://stackoverflow.com/questions/36835671/laravel-5-unit-testing-status-code-500-expected-200

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!