ModelFactory:错误:在null上调用成员函数connection()

时间:2018-09-20 11:56:00

标签: laravel eloquent phpunit lumen

在尝试使用ModelFactory填充数据库时,我遇到了这一错误,但自最近六个小时以来一直在查找,似乎无法修复。

我的测试运行此:

$books = factory('App\Book', 2)->create();

,错误消息是这样的:

There was 1 error:

1) Tests\App\Http\Controllers\BooksControllerTest::testIndexReturnsCollection
Error: Call to a member function connection() on null

D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:1234
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:1200
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:1030
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:945
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:983
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\FactoryBuilder.php:203
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\support\Collection.php:407
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\FactoryBuilder.php:207
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\FactoryBuilder.php:185
D:\Code\Server\htdocs\rlfp.localhost.com\tests\app\Http\Controllers\BooksControllerTest.php:35

在bootstrap / app.php上,我位于第26和28行,如下所示:

$app->withFacades();

$app->withEloquent();

虽然我的phpunit.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/app.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_DATABASE" value="rlp_rlfpsfactory"/>
    </php>
</phpunit>

我是否需要在phpunit xml文件的php部分中放入其他内容?

1 个答案:

答案 0 :(得分:0)

度过了整整一天的运气后,由于@ChrisSprague,我将Lumen降级为5.6,并且有效。

步骤:

  1. 在composer.json文件中更改

    "laravel/lumen-framework": "5.7.*"
    

    "laravel/lumen-framework": "5.6.*"
    
  2. 删除您的vendor文件夹

  3. 执行composer install
  4. 再次运行测试!

如果仍然无法正常运行,请确保您的phpunit.xml文件中具有正确的测试数据库详细信息

我的样子:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/app.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="DB_DATABASE" value="rlp_rlfpsfactory"/> <!-- this is testing database -->
        <env name="DB_USERNAME" value="root"/>
        <env name="DB_PASSWORD" value=""/>
    </php>
</phpunit>