Laravel Model Factory without connection to database

感情迁移 提交于 2021-02-18 10:12:23

问题


I would like to use Laravel's Model Factory in some PHPUnit tests. The only thing I want to do is make a Model instance without saving it to database.

Why the Model Factory needs connection to database? These tests must pass on CI environment without configured database.

When I create Model manually by new App\Model($dataArray), tests pass and the connection is not needed.

I am using Model Factory in other places, so I would like to reuse it in that tests, to avoid code duplication.

I am using MongoDB and jenssegers/laravel-mongodb library, but I am guessing that it has no matter - in pure Eloquent and e.g. MySQL database, the issue would be the same.

Test that works without database:

class ModelTransformerTest extends TestCase
{
    public function testTransformMinimalModelData()
    {
        $data = [
            '_id' => $faker->md5,
            'email' => $faker->email,
        ];

        $model = new App\Model($data);
        // […];
    }
}

My Model Factory

$factory->defineAs(Model::class, 'base', function ($faker) {
    return [
        '_id' => $faker->md5,
        'email' => $faker->email,
    ];
});

Test that needs database connection:

class ModelTransformerTest extends TestCase
{
    public function testTransformMinimalModelData()
    {
        $model = factory(App\Model::class, 'base')->make();
        // […];
    }
}

Full stack trace:

Error: Class 'MongoDB\Driver\Manager' not found

app\vendor\mongodb\mongodb\src\Client.php:56
app\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\Connection.php:147
app\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\Connection.php:37
app\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\MongodbServiceProvider.php:27
app\vendor\illuminate\database\DatabaseManager.php:173
app\vendor\illuminate\database\DatabaseManager.php:68
app\vendor\illuminate\database\Eloquent\Model.php:3282
app\vendor\illuminate\database\Eloquent\Model.php:3248
app\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\Eloquent\Model.php:523
app\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\Eloquent\Model.php:284
app\vendor\illuminate\database\Eloquent\Model.php:443
app\vendor\illuminate\database\Eloquent\Model.php:281
app\vendor\illuminate\database\Eloquent\FactoryBuilder.php:142
app\vendor\illuminate\database\Eloquent\Model.php:2286
app\vendor\illuminate\database\Eloquent\FactoryBuilder.php:143
app\vendor\illuminate\database\Eloquent\FactoryBuilder.php:106
app\tests\phpunit\Transformers\ModelTransformerTest.php:25
\php\pear\PHPUnit\TextUI\Command.php:176
\php\pear\PHPUnit\TextUI\Command.php:129

回答1:


I don't think this is currently possible. The make() method calls newModelInstance() which tries to set a database connection.

You can review the source code yourself.




回答2:


Do you try using DatabaseTransactions?

For example

use Illuminate\Foundation\Testing\DatabaseTransactions;
class ModelTransformerTest extends TestCase
{
    use DatabaseTransactions;
}



回答3:


I normally go for creating a similar test DB and run all the tests on that DB.

It ensures that your test DB type is same as production and you don't touch the production DB for running the tests.



来源:https://stackoverflow.com/questions/38882594/laravel-model-factory-without-connection-to-database

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