Laravel 5 在laravel 5.5中使用内存数据库
我打算使用内存中的数据库进行单元测试在laravel。。。 我在phpunit.xml中添加了这一行Laravel 5 在laravel 5.5中使用内存数据库,laravel-5,Laravel 5,我打算使用内存中的数据库进行单元测试在laravel。。。 我在phpunit.xml中添加了这一行 <php> <env name="APP_ENV" value="testing"/> <env name="CACHE_DRIVER" value="array"/> <env name="SESSION_DRIVER" value="array"/> <env name="Q
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite" />
<env name="DB_DATABASE" value=":memory:" />
</php>
这是我在\tests\Feature\BookTest.php目录下的BookTest.php文件
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class BookTest extends TestCase
{
use DatabaseMigrations;
public function test_books_can_be_created()
{
$user = factory(\App\User::class)->create();
$book = $user->books()->create([
'name' => 'The hobbit',
'price' => 10
]);
$found_book = $book->find(1);
$this->assertEquals($found_book->name, 'The hobbit');
$this->assertEquals($found_book->price, 10);
}
}
您应该
use RefreshDatabase
所以你的测试应该是这样的
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class BookTest extends TestCase
{
use RefreshDatabase;
public function test_books_can_be_created()
{
$user = factory(\App\User::class)->create();
$book = $user->books()->create([
'name' => 'The hobbit',
'price' => 10
]);
$found_book = $book->find(1);
$this->assertEquals($found_book->name, 'The hobbit');
$this->assertEquals($found_book->price, 10);
}
}
您应该
use RefreshDatabase
所以你的测试应该是这样的
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class BookTest extends TestCase
{
use RefreshDatabase;
public function test_books_can_be_created()
{
$user = factory(\App\User::class)->create();
$book = $user->books()->create([
'name' => 'The hobbit',
'price' => 10
]);
$found_book = $book->find(1);
$this->assertEquals($found_book->name, 'The hobbit');
$this->assertEquals($found_book->price, 10);
}
}
您有创建图书
表的迁移吗?我记得没有。我也需要迁移吗?。真的很抱歉,如果您有运行迁移的“使用数据库迁移”,那么您有创建books
表的迁移吗?我记得没有。我也需要迁移吗?。如果您有运行迁移的“使用databasemigrations”,真的很抱歉