Laravel 5 - 单元测试 - 状态码 500,预期为 200
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36835671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 - Unit testing - status code 500, expected 200
提问by Dulo
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 ?
为什么“测试 1”中的单元测试返回状态代码 500,而不是 200?有人可以解释我吗?这是针对相同操作的 2 个测试中的示例,它们返回不同的状态代码。我预计两次测试都是 200?
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:
cmd 中的响应:
回答by Dulo
I checked laravel.log and I found next logs:
我检查了 laravel.log 并找到了下一个日志:
[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:
[2016-04-26 08:45:49] testing.ERROR: 异常“ErrorException”,消息“试图获取非对象的属性”在 C:\xampp\htdocs\STP\storage\framework\views\76c117f88e2ab8d2c5f85f545873e2. php:7 堆栈跟踪:
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:
C:\xampp\htdocs 中带有消息“试图获取非对象的属性(视图:C:\xampp\htdocs\STP\resources\views\admin\languages\showAll.blade.php)”的下一个异常“ErrorException” \STP\storage\framework\views\76c117f88e2ab8d2c5f85f5187e254573559a2c3.php:7
堆栈跟踪:
and on my view i access to $language properties with :
在我看来,我可以通过以下方式访问 $language 属性:
$languages->char, $language->name
$languages->char, $language->name
but it is array so I should access with:
但它是数组所以我应该访问:
$language['char'], $language['name']
$language['char'], $language['name']
and both tests now work properly and returns status code 200.
并且两个测试现在都可以正常工作并返回状态代码 200。
Thanks all for help.
感谢大家的帮助。