【问题标题】:How can test a function that gets data from DB in a Laravel 5 package?如何测试从 Laravel 5 包中的数据库获取数据的函数?
【发布时间】:2019-08-08 00:26:34
【问题描述】:

我正在开发一个 Laravel 5 包,并编写测试我正在尝试测试一个从 DB 获取数据的函数。

public function getPhotoDatasFromDb()
{
    $ret = GalleryImage::get()->keyBy('file_name');
    return $ret;
}

返回值应采用以下格式:

Collection {#416 ▼
  #items: array:2 [▼
    "IMG_1979.jpg" => GalleryImage {#423 ▼}
        "alt_text" => "example alt text"
        "description" => "lorem ipsum"
    "IMG_1980.jpg" => GalleryImage {#424 ▶}
  ]
}

我已经有在其他 Laravel 应用程序中测试数据库测试的经验。

我的问题是:因为我正在编写一个包,并且在开发环境中我没有数据库实例,所以我想知道测试它的最佳方法是什么?

如果有助于了解更广泛的情况,数据库表将通过此迁移在应用程序中创建:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGalleryImagesTable extends Migration
{
    public function up()
    {
        Schema::create('gallery_images', function (Blueprint $table) {
            $table->increments('id');
            $table->string('file_name')->unique();
            $table->text('description')->nullable();
            $table->string('alt')->nullable();
            $table->string('video_link')->nullable();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('gallery_images');
    }
}

这是关联的模型

<?php

namespace DavideCasiraghi\ResponsiveGallery;

use Illuminate\Database\Eloquent\Model;

class GalleryImage extends Model
{
    protected $fillable = [
        'file_name', 'description', 'alt', 'video_link',
    ];
}

【问题讨论】:

  • 您实际上是在尝试测试 get() 方法是否按预期工作,还是在尝试测试您的代码是否将预期数据保存到数据库中?
  • 感谢您的提问,它帮助我弄清楚了一些问题。我需要测试 get() 方法是否按方面工作。

标签: laravel unit-testing laravel-5 phpunit


【解决方案1】:

我自己找到了解决方案。 我发布它以防它对其他人有帮助。

这是我的测试:

    /** @test */
    public function it_gets_photos_from_db()
    {
        $gallery = new ResponsiveGalleryFactory();
        $dbImageDatas = $gallery->getPhotoDatasFromDb();
        $this->assertStringContainsString($dbImageDatas['DSC_9470.jpg']->description, 'Photo description');
    }

为了让它工作,我必须在测试类开始时配置数据库:

    /**
     * Create the tables this model needs for testing.
     */
    public static function setUpBeforeClass() : void
    {
        $capsule = new Capsule;

        $capsule->addConnection([
            'driver' => 'sqlite',
            'database' => ':memory:',
            'prefix' => '',
        ]);

        $capsule->setAsGlobal();
        $capsule->bootEloquent();

        Capsule::schema()->create('gallery_images', function (Blueprint $table) {
            $table->increments('id');
            $table->string('file_name')->unique();
            $table->text('description')->nullable();
            $table->string('alt')->nullable();
            $table->string('video_link')->nullable();
            $table->timestamps();
        });

        Model::unguard();

        GalleryImage::create([
            'file_name' => 'DSC_9470.jpg',
            'description' => 'Photo description',
            'alt_text' => 'Photo alt text',
            'video_link' => 'https://www.youtube.com/fsda234',
        ]);
    } 

【讨论】:

    猜你喜欢
    • 2015-07-19
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2014-12-19
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    相关资源
    最近更新 更多