Laravel 测试驱动开发 -- 正向单元测试

CRUD Unit Testing in Laravel

上一篇翻译的文章里通过简单的十一步讲述了如何在Laravel中进行测试驱动开发,这是作者关于测试的第二篇文章, 文章中简述了如何在Laravel中进行增删改查的单元测试,本文中的单元测试都是正向测试,之后还会有一篇来讲述如何做反向测试。

正向测试: Positive test 提供合法数据,测试预期被测程序能得到正常执行。

反向测试:Negative test 提供非法的输入数据,测试预期被测程序抛出指定的Exception。

译文链接:https://medium.com/@jsdecena/crud-unit-tes...

以下是译文:

最近我开启了一个开源电子商务应用项目LARACOM, 使用的是Laravel框架并集成了Rob Gloudeman 的shoping cart 和与其相关的packages。

在开启这个项目时我必须为项目做长远规划和考虑,所以在我脑海中从来就没出现过"我不会用到TDD(test driven development)方法"这个想法,TDD是开发的必选项。为了进行TDD,我需要将项目中的测试用例划分到两个不同的组中,一组是单元测试另外一组是功能测试。

单元测试是用来测试项目中的Model、Repository等等这些类的,而功能测试是看测试代码是否能够正确访问到Controller并断言Controller的行为:是否redirect 到了目标URL、返回了指定的视图或者是跳转并携带着造成跳转的错误信息。

介绍的足够多了,如果你之前没有尝试过TDD,我之前有写进行TDD的基本方法。那篇文章里有介绍TDD的基本概念和开始TDD的基本方法,所以这里不再赘述。

今天我们要做的是为我的项目写Carousel业务的基本CRUD方法。

译者注:文章里作者开推荐了他写的实现repository设计模式的package

Edit: Hey! I created a base repository package you can use for your next project :)

Part I: Positive Unit Testing

从create测试开始

让我们从对create操作的测试开始。

创建/tests/Unit/Carousels/CarouselUnitTest.php 文件

注:通过命令 php artisan make:test Carousels/CarouselUnitTest --unit 创建

<?php
namespace Tests\Unit\Carousels;
use Tests\TestCase;
class CarouselUnitTest extends TestCase
{
    /** @test */
    public function it_can_create_a_carousel()
    {
        $data = [
            'title' => $this->faker->word,
            'link' => $this->faker->url,
            'src' => $this->faker->url,
        ];

        $carouselRepo = new CarouselRepository(new Carousel);
        $carousel = $carouselRepo->createCarousel($data);

        $this->assertInstanceOf(Carousel::class, $carousel);
        $this->assertEquals($data['title'], $carousel->title);
        $this->assertEquals($data['link'], $carousel->link);
        $this->assertEquals($data['image_src'], $carousel->src);
    }
}

在这个文件中我们要做的是:

  • 测试respository类是否能够用这些参数在数据库中新建carousel记录。
  • 测试在新建carousel后,返回的carousel对象各属性的值是否与创建时提供的参数值一致。

现在在你的终端中,执行vendor/bin/phpunit(当前工作目录必须是你项目的根目录)。

是否有错误?是的,因为我们还没有创建CarouselRepository.php这个文件,所以会有如下错误

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
E                                                                   1 / 1 (100%)
Time: 700 ms, Memory: 26.00MB
There was 1 error:
1) Tests\Unit\Carousels\CarouselUnitTest::it_can_create_a_carousel
Error: Class 'Tests\Unit\Carousels\CarouselRepository' not found

让我们创建app/Shop/Carousels/Repositories/CarouselRepository.php文件

<?php
namespace App\Shop\Carousels\Repositories;
use App\Shop\Carousels\Carousel;
use App\Shop\Carousels\Exceptions\CreateCarouselErrorException;
use Illuminate\Database\QueryException;
class CarouselRepository
{
    /**
     * CarouselRepository constructor.
     * @param Carousel $carousel
     */
    public function __construct(Carousel $carousel)
    {
        $this->model = $carousel;
    }
    /**
     * @param array $data
     * @return Carousel
     * @throws CreateCarouselErrorException
     */
    public function createCarousel(array $data) : Carousel
    {
        try {
            return $this->model->create($data);
        } catch (QueryException $e) {
            throw new CreateCarouselErrorException($e);
        }
    }
}

在这个repository中,我们用依赖注入将CarouselModel注入到了其中,因为还没有这个CarouselModel所以在注入这个Model时会抛出一个错误。

我们先来创建: app/Shop/Carousels/Carousel.php

<?php
namespace App\Shop\Carousels;
use Illuminate\Database\Eloquent\Model;
class Carousel extends Model
{
    protected $fillable = [
        'title',
        'link',
        'src'
    ];
}

在repository创建完成后,我们将其引入到我们的测试文件中去,像这样:

<?php
namespace Tests\Unit\Carousels;
use App\Shop\Carousels\Carousel;
use App\Shop\Carousels\Repositories\CarouselRepository;
use Tests\TestCase;
class CarouselUnitTest extends TestCase
{
    /** @test */
    public function it_can_create_a_carousel()
    {
        $data = [
            'title' => $this->faker->word,
            'link' => $this->faker->url,
            'src' => $this->faker->url,
        ];

        $carouselRepo = new CarouselRepository(new Carousel);
        $carousel = $carouselRepo->createCarousel($data);

        $this->assertInstanceOf(Carousel::class, $carousel);
        $this->assertEquals($data['title'], $carousel->title);
        $this->assertEquals($data['link'], $carousel->link);
        $this->assertEquals($data['image_src'], $carousel->src);
    }
}

接下来在此运行vendor/bin/phpunit

Error Again? Yes? 你猜对了。

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
E                                                                   1 / 1 (100%)
Time: 898 ms, Memory: 26.00MB
There was 1 error:
1) Tests\Unit\Carousels\CarouselUnitTest::it_can_create_a_carousel
App\Shop\Carousels\Exceptions\CreateCarouselErrorException: PDOException: SQLSTATE[HY000]: General error: 1 no such table: carousels in /Users/jsd/Code/shop/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:77

测试文件中尝试往carousels表中写入数据,但是这个表现在还不存在。我们接下来通过Migration文件来创建这个表。

在命令行终端中运行:

php artisan make:migration create_carousels_table --create=carousels

编辑迁移文件如下:


<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCarouselTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('carousels', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('link')->nullable();
            $table->string('src');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('carousels');
    }
}

link字段是可空的,titleimage字段是必须项。

执行迁移文件创建完carousels表后,再次执行vendor/bin/phpunit显示如下:

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
.                                                                   1 / 1 (100%)
Time: 696 ms, Memory: 26.00MB
OK (1 test, 6 assertions)

现在你已经让这个测试通过了,所以只要你在运行vendor/bin/phpunit时这个测试能正确通过,那么你就能认为会应用能成功创建carousel记录。

read测试

现在让我们来测试在创建carousel后,是否能从正确的读取到它。

<?php
namespace Tests\Unit\Carousels;
use Tests\TestCase;
class CarouselUnitTest extends TestCase
{
    /** @test */
    public function it_can_show_the_carousel()
    {
        $carousel = factory(Carousel::class)->create();
        $carouselRepo = new CarouselRepository(new Carousel);
        $found = $carouselRepo->findCarousel($carousel->id);

        $this->assertInstanceOf(Carousel::class, $found);
        $this->assertEquals($found->title, $carousel->title);
        $this->assertEquals($found->link, $carousel->link);
        $this->assertEquals($found->src, $carousel->src);
    }
    /** @test */
    public function it_can_create_a_carousel()
    {
        $data = [
            'title' => $this->faker->word,
            'link' => $this->faker->url,
            'src' => $this->faker->url,
        ];

        $carouselRepo = new CarouselRepository(new Carousel);
        $carousel = $carouselRepo->createCarousel($data);

        $this->assertInstanceOf(Carousel::class, $carousel);
        $this->assertEquals($data['title'], $carousel->title);
        $this->assertEquals($data['link'], $carousel->link);
        $this->assertEquals($data['src'], $carousel->src);
    }
}

运行测试,在每次我们新建测试后,我们总是预期测试会运行失败,为什么呢?因为我们还没有实现逻辑。如果我们在创建完测试后运行既能得到success的结果,那么证明我们在应用TDD时步骤上出现了错误(TDD 先写测试后编码实现)。

每一个我们新建的测试都要放在测试文件中的头部,因为我们想让新建的测试优先运行

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
E                                                                   1 / 1 (100%)
Time: 688 ms, Memory: 26.00MB
There was 1 error:
1) Tests\Unit\Carousels\CarouselUnitTest::it_can_show_the_carousel
InvalidArgumentException: Unable to locate factory with name [default] [App\Shop\Carousels\Carousel].

在这个错误中显示,测试程序尝试调用了还不存在的模型工厂。

创建文件: database/factories/CarouselModelFactory.php

<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
use App\Shop\Carousels\Carousel;
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(Carousel::class, function (Faker\Generator $faker) {
    return [
        'title' => $faker->word,
        'link' => $faker->url,
        'src' => $faker->url,
    ];
});

在此运行phpunit

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
E                                                                   1 / 1 (100%)
Time: 708 ms, Memory: 26.00MB
There was 1 error:
1) Tests\Unit\Carousels\CarouselUnitTest::it_can_show_the_carousel
Error: Call to undefined method App\Shop\Carousels\Repositories\CarouselRepository::findCarousel()

现在找不到模型工厂的错误消失了,意味着现在可以持久化到数据库里了,有些人想让创建对象和持久化的过程能够分开,那么可以将测试代码中的:

$carousel = factory(Carousel::class)->create();

替换成:

$carousel = factory(Carousel::class)->make();

但是现在测试程序中仍然有错误,因为在repository中找不到findCarousel()方法。

<?php
namespace App\Shop\Carousels\Repositories;
use App\Shop\Carousels\Carousel;
use App\Shop\Carousels\Exceptions\CarouselNotFoundException;
use App\Shop\Carousels\Exceptions\CreateCarouselErrorException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
class CarouselRepository
{
   protected $model;

    /**
     * CarouselRepository constructor.
     * @param Carousel $carousel
     */
    public function __construct(Carousel $carousel)
    {
        $this->model = $carousel;
    }
    /**
     * @param array $data
     * @return Carousel
     * @throws CreateCarouselErrorException
     */
    public function createCarousel(array $data) : Carousel
    {
        try {
            return $this->model->create($data);
        } catch (QueryException $e) {
            throw new CreateCarouselErrorException($e);
        }
    }
    /**
     * @param int $id
     * @return Carousel
     * @throws CarouselNotFoundException
     */
    public function findCarousel(int $id) : Carousel
    {
        try {
            return $this->model->findOrFail($id);
        } catch (ModelNotFoundException $e) {
            throw new CarouselNotFoundException($e);
        }
    }
}

现在运行phpunit看看输出是什么。

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
.                                                                   1 / 1 (100%)
Time: 932 ms, Memory: 26.00MB
OK (1 test, 6 assertions)

update测试

现在让我们测试一下是否能够对carousel进行更新

<?php
namespace Tests\Unit\Carousels;
use Tests\TestCase;
class CarouselUnitTest extends TestCase
{
    /** @test */
    public function it_can_update_the_carousel()
    {
        $carousel = factory(Carousel::class)->create();

        $data = [
            'title' => $this->faker->word,
            'link' => $this->faker->url,
            'src' => $this->faker->url,
        ];

        $carouselRepo = new CarouselRepository($carousel);
        $update = $carouselRepo->updateCarousel($data);

        $this->assertTrue($update);
        $this->assertEquals($data['title'], $carousel->title);
        $this->assertEquals($data['link'], $carousel->link);
        $this->assertEquals($data['src'], $carousel->src);
    }
    /** @test */
    public function it_can_show_the_carousel()
    {
        $carousel = factory(Carousel::class)->create();
        $carouselRepo = new CarouselRepository(new Carousel);
        $found = $carouselRepo->findCarousel($carousel->id);

        $this->assertInstanceOf(Carousel::class, $found);
        $this->assertEquals($found->title, $carousel->title);
        $this->assertEquals($found->link, $carousel->link);
        $this->assertEquals($found->src, $carousel->src);
    }
    /** @test */
    public function it_can_create_a_carousel()
    {
        $data = [
            'title' => $this->faker->word,
            'link' => $this->faker->url,
            'src' => $this->faker->url,
        ];

        $carouselRepo = new CarouselRepository(new Carousel);
        $carousel = $carouselRepo->createCarousel($data);

        $this->assertInstanceOf(Carousel::class, $carousel);
        $this->assertEquals($data['title'], $carousel->title);
        $this->assertEquals($data['link'], $carousel->link);
        $this->assertEquals($data['src'], $carousel->src);
    }
}

这里我们在吃使用模型工厂创建了carousel记录,然后通过$data参数给updateCarousel来更新carousel并断言更新后的carousel对象的属性值与$data中的字段值一样。

现在这个测试会运行失败,因为你知道的在repository类中还没有定义updateCarousel方法,现在让我们来创建这个方法。

<?php
namespace App\Shop\Carousels\Repositories;
use App\Shop\Carousels\Carousel;
use App\Shop\Carousels\Exceptions\CarouselNotFoundException;
use App\Shop\Carousels\Exceptions\CreateCarouselErrorException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
class CarouselRepository
{
   protected $model;

    /**
     * CarouselRepository constructor.
     * @param Carousel $carousel
     */
    public function __construct(Carousel $carousel)
    {
        $this->model = $carousel;
    }
    /**
     * @param array $data
     * @return Carousel
     * @throws CreateCarouselErrorException
     */
    public function createCarousel(array $data) : Carousel
    {
        try {
            return $this->model->create($data);
        } catch (QueryException $e) {
            throw new CreateCarouselErrorException($e);
        }
    }
    /**
     * @param int $id
     * @return Carousel
     * @throws CarouselNotFoundException
     */
    public function findCarousel(int $id) : Carousel
    {
        try {
            return $this->model->findOrFail($id);
        } catch (ModelNotFoundException $e) {
            throw new CarouselNotFoundException($e);
        }
    }

     /**
     * @param array $data
     * @return bool
     * @throws UpdateCarouselErrorException
     */
    public function updateCarousel(array $data) : bool
    {
        try {
            return $this->model->update($data);
        } catch (QueryException $e) {
            throw new UpdateCarouselErrorException($e);
        }
    }
}

updateCarousel()方法创建完后再次运行phpunit

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
.                                                                   1 / 1 (100%)
Time: 932 ms, Memory: 26.00MB
OK (1 test, 6 assertions)

方法创建完后测试立马就能运行成功了。

delete测试

最后让我们看一下删除carousel测试

<?php
namespace Tests\Unit\Carousels;
use Tests\TestCase;
class CarouselUnitTest extends TestCase
{
    /** @test */
    public function it_can_delete_the_carousel()
    {
        $carousel = factory(Carousel::class)->create();

        $carouselRepo = new CarouselRepository($carousel);
        $delete = $carouselRepo->deleteCarousel();

        $this->assertTrue($delete);
    }

    /** @test */
    public function it_can_update_the_carousel()
    {
        $carousel = factory(Carousel::class)->create();

        $data = [
            'title' => $this->faker->word,
            'link' => $this->faker->url,
            'src' => $this->faker->url,
        ];

        $carouselRepo = new CarouselRepository($carousel);
        $update = $carouselRepo->updateCarousel($data);

        $this->assertTrue($update);
        $this->assertEquals($data['title'], $carousel->title);
        $this->assertEquals($data['link'], $carousel->link);
        $this->assertEquals($data['src'], $carousel->src);
    }
    /** @test */
    public function it_can_show_the_carousel()
    {
        $carousel = factory(Carousel::class)->create();
        $carouselRepo = new CarouselRepository(new Carousel);
        $found = $carouselRepo->findCarousel($carousel->id);

        $this->assertInstanceOf(Carousel::class, $found);
        $this->assertEquals($found->title, $carousel->title);
        $this->assertEquals($found->link, $carousel->link);
        $this->assertEquals($found->src, $carousel->src);
    }
    /** @test */
    public function it_can_create_a_carousel()
    {
        $data = [
            'title' => $this->faker->word,
            'link' => $this->faker->url,
            'src' => $this->faker->url,
        ];

        $carouselRepo = new CarouselRepository(new Carousel);
        $carousel = $carouselRepo->createCarousel($data);

        $this->assertInstanceOf(Carousel::class, $carousel);
        $this->assertEquals($data['title'], $carousel->title);
        $this->assertEquals($data['link'], $carousel->link);
        $this->assertEquals($data['src'], $carousel->src);
    }
}

然后在repository中创建deleteCarousel()方法:

<?php
namespace App\Shop\Carousels\Repositories;
use App\Shop\Carousels\Carousel;
use App\Shop\Carousels\Exceptions\CarouselNotFoundException;
use App\Shop\Carousels\Exceptions\CreateCarouselErrorException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
class CarouselRepository
{
   protected $model;

    /**
     * CarouselRepository constructor.
     * @param Carousel $carousel
     */
    public function __construct(Carousel $carousel)
    {
        $this->model = $carousel;
    }
    /**
     * @param array $data
     * @return Carousel
     * @throws CreateCarouselErrorException
     */
    public function createCarousel(array $data) : Carousel
    {
        try {
            return $this->model->create($data);
        } catch (QueryException $e) {
            throw new CreateCarouselErrorException($e);
        }
    }
    /**
     * @param int $id
     * @return Carousel
     * @throws CarouselNotFoundException
     */
    public function findCarousel(int $id) : Carousel
    {
        try {
            return $this->model->findOrFail($id);
        } catch (ModelNotFoundException $e) {
            throw new CarouselNotFoundException($e);
        }
    }

     /**
     * @param array $data
     * @return bool
     * @throws UpdateCarouselErrorException
     */
    public function updateCarousel(array $data) : bool
    {
        try {
            return $this->model->update($data);
        } catch (QueryException $e) {
            throw new UpdateCarouselErrorException($e);
        }
    }

    /**
    * @return bool
    */
    public function deleteCarousel() : bool
    {
        return $this->model->delete();
    }
}

当这个方法被执行后,它应返回布尔值。如果删除成功返回True,否则返回null。然后再次运行phpunit

➜ git: phpunit --filter=CarouselUnitTest::it_can_delete_the_carousel
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
.                                                                   1 / 1 (100%)
Time: 916 ms, Memory: 26.00MB
OK (1 test, 1 assertion)

WOW. 现在你已经大功告成了CONGRATULATIONS!

如果想看更多的测试Example可以我的项目的单元测试目录

下一部分将讲述在Laravel中进行Negative Unit Testing。

本作品采用《CC 协议》,转载必须注明作者和本文链接
公众号:网管叨bi叨 | Golang、Laravel、Docker、K8s等学习经验分享
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 2

哈哈哈,最近正想着学习测试驱动开发,就看见这篇文章!
谢谢大佬!

5年前 评论

喜欢偏实战的学习,感谢翻译!

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
113
粉丝
365
喜欢
484
收藏
314
排名:34
访问:20.4 万
私信
所有博文
社区赞助商