10.测试重构

未匹配的标注

本系列文章为laracasts.com 的系列视频教程——Testing Laravel 的学习笔记。若喜欢该系列视频,可去该网站订阅后下载该系列视频,支持正版

本节说明

  • 对应第 10 小节:Test Method Refactoring

本节内容

本节我们来对上一节的测试进行些重构。在上一节的测试中,对于每一个测试,我们都重复了两个动作的代码:

  1. 获取Post实例;
  2. 登录User

我们可以借助setUp方法来进行重构:

tests\Unit\LikesTest.php

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class LikesTest extends TestCase
{
    use RefreshDatabase;

    public function setUp()
    {
        parent::setUp();

        $this->post = factory('App\Post')->create();
    }
    .
    .
}

这样的话,我们获取Post实例的代码就不必重复。对于用户登录的操作,在很多的测试中我们都将用的,所以我们可以把逻辑放到TestCase基类中:

tests\TestCase.php

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected $user;

    public function signIn($user = null)
    {
        if(! $user){
            $user = factory('App\User')->create();
        }

        $this->actingAs($user);

        $this->user = $user;

        return $this;
    }
}

最终我们重构后的代码如下:

tests\Unit\LikesTest.php

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class LikesTest extends TestCase
{
    use RefreshDatabase;

    public function setUp()
    {
        parent::setUp();

        $this->post = factory('App\Post')->create();

        $this->signIn();
    }

    /** @test */
    public function a_user_can_like_a_post()
    {
        $this->post->like();

        $this->assertDatabaseHas('likes',[
           'user_id' => $this->user->id,
           'likeable_id' => $this->post->id,
           'likeable_type' => get_class($this->post),
        ]);

        $this->assertTrue($this->post->isLiked());
    }

    /** @test */
    public function a_user_can_unlike_a_post()
    {
        $this->post->like();
        $this->post->unlike();

        $this->assertDatabaseMissing('likes',[
           'user_id' => $this->user->id,
           'likeable_id' => $this->post->id,
           'likeable_type' => get_class($this->post),
        ]);

        $this->assertFalse($this->post->isLiked());
    }

    /** @test */
    public function a_user_may_toggle_a_posts_like_status()
    {
        $this->post->toggle();
        $this->assertTrue($this->post->isLiked());

        $this->post->toggle();
        $this->assertFalse($this->post->isLiked());
    }

    /** @test */
    public function a_post_knows_how_many_likes_it_has()
    {
        $this->post->toggle();
        $this->assertEquals(1,$this->post->likesCount);
    }
}

然后运行测试:
file
当然,我们也可以创建测试用的辅助函数文件,然后将登录用户的代码放到辅助函数文件中。辅助函数文件的好处是可以帮我们简化一些很常用的代码,例如factory('App\Post')->create()我们就可以放到辅助函数文件的create方法中:

tests\helpers\functions.php

<?php

function create($class,$attributes = [])
{
    return factory($class)->create($attributes);
}

修改composer.json文件:

    .
    .
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        },
        "files":[
            "tests/helpers/functions.php"
        ]
    },
    .
    .

然后自动加载该文件:

$ composer dump-autoload

现在我们可以像下面这样调用:

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class LikesTest extends TestCase
{
    use RefreshDatabase;

    public function setUp()
    {
        parent::setUp();

        $this->post = create('App\Post');

        $this->signIn();
    }
    .
    .
}

再次测试:
file

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~