Laravel 5 - Unit testing - status code 500, expected 200
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.
Dulo
Updated on June 04, 2022Comments
-
Dulo almost 2 years
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: