Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5 在laravel 5.5中使用内存数据库_Laravel 5 - Fatal编程技术网

Laravel 5 在laravel 5.5中使用内存数据库

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

我打算使用内存中的数据库进行单元测试在laravel。。。 我在phpunit.xml中添加了这一行

<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”,真的很抱歉