【发布时间】:2015-09-12 20:44:51
【问题描述】:
我开始使用 phpunit 向旧版 Laravel 4.2 webapp 添加单元/功能测试,当使用 DB::table 时我看到一个奇怪的错误。
这是一个非常简单的例子,测试命中了一个控制器方法,该方法调用DB::table 然后死掉并转储结果。
class ExternalFormTest extends TestCase {
public function testGetExternalFormThankYouPage()
{
$response = $this->call('GET', 'test');
这是被命中的控制器方法。
public function getIndex()
{
$results = DB::table('users')->get();
dd($results);
这将返回一个数组数组。
..array(12) {
[0] =>
array(74) {
'id' =>
int(1)
[0] =>
int(1)
'account_number' =>
int(1000)
[1] =>
int(1000)
'account_admin' =>
int(1)
但如果我用浏览器点击它。
我得到一个对象数组...
array (size=12)
0 =>
object(stdClass)[1967]
public 'id' => int 1
public 'account_number' => int 1000
public 'account_admin' => int 1
public 'user_type' => int 1
这会导致整个应用程序出现大量错误。因为代码期望访问属性(即$individual_result->id)但结果是数组。我已经在内存和普通 MySql 数据库中使用 sqlite 进行了尝试。这是一个错误还是我错过了有关 Laravel 如何返回结果和/或 phpunit 如何工作的内容。
任何建议都会有所帮助。
这是我的 phpunit.xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./app/tests/</directory>
</testsuite>
</testsuites>
</phpunit>
【问题讨论】:
标签: php mysql unit-testing laravel-4 phpunit