2 测试发贴 phpunit tdd

安装相关插件
https://github.com/sempro/phpunit-pretty-print

composer require sempro/phpunit-pretty-print --dev

修改phpunit.xml为如下

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="MAIL_DRIVER" value="array"/>
    </php>
</phpunit>

试着运行一下

➜  blog.dock.com phpunit
PHPUnit 7.4.3 by Sebastian Bergmann and contributors.


Tests\Unit\ExampleTest
  ✓ basic test [0.635s]

Tests\Feature\ExampleTest
  ✓ basic test [0.265s]


Time: 1.39 seconds, Memory: 14.00MB

OK (2 tests, 2 assertions)

so cool
接下来开始真正的测试吧
parent::setUp();必不可少,防止父方法被覆盖

<?php

namespace Tests\Feature;

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

class ThreadTest extends TestCase
{
    use DatabaseMigrations;

    protected $thread;

    public function setUp()
    {
        parent::setUp();
        $this->thread = factory(\App\Models\Thread::class)->create();
    }
    /**
     * 用户可以看见所有帖子
     * @test
     * @return void
     */
    public function user_can_view_all_threads()
    {
        $response = $this->get('/threads');
        $response->assertStatus(200);
        $response->assertSee($this->thread->title);
    }

    /**
     * 用户可以看见帖子详情页
     * @test
     * @return void
     */
    public function user_can_view_an_thread()
    {
        $response = $this->get($this->thread->path());
        $response->assertStatus(200);
        $response->assertSee($this->thread->title);
    }

}

测试 失败 挖坑待填

➜  blog.dock.com phpunit
PHPUnit 7.4.3 by Sebastian Bergmann and contributors.

Tests\Feature\ThreadTest
  x user can view all threads [0.024s]
  x user can view an thread [0.000s]

开始修复问题 填坑
增加如下两行route

Route::get('/threads', 'ThreadController@index');
Route::get('/threads/{thread}', 'ThreadController@show')

model增加path方法

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Thread extends Model
{
    //
    public function path()
    {
        return '/threads/'.$this->id;
    }
}

控制器 查看所有 index方法

public function index()
{
        //
        $threads = Thread::latest()->get();

        return view('threads.index', compact('threads'));
 }

相关视图文件

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Forum Threads</div>

                <div class="card-body">
                    @foreach ($threads as $thread)
                        <article>
                            <h4><a href="{{ $thread->path() }}" target="_blank">{{ $thread->title }}</a></h4>
                            <div class="body">{{ $thread->body }}</div>
                        </article>
                        <hr>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

测试通过

➜  blog.dock.com phpunit
PHPUnit 7.4.3 by Sebastian Bergmann and contributors.

Tests\Feature\ThreadTest
  ✓ user can view all threads [0.752s]
  x user can view an thread [0.113s]

处理查看单个详情页

 public function show(Thread $thread)
    {
        //
        return view('threads.show', compact('thread'));
    }

相关的视图代码

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Some Thread</div>

                    <div class="card-body">
                            <article>
                                <h4>{{ $thread->title }}</h4>
                                <div class="body">{{ $thread->body }}</div>
                            </article>
                            <hr>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

测试通过,大功告成!

Tests\Feature\ThreadTest
  ✓ user can view all threads [0.660s]
  ✓ user can view an thread [0.112s]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • 上一篇:JBPopup的创建下一篇:ProjectViewPopupMenu的创建 插件已发布 1,在Plugin...
    街头客阅读 6,992评论 0 8
  • 文/孤鸟差鱼 一场哑剧 撑起的华丽 惊呆了众人
    孤鸟差鱼阅读 189评论 0 2
  • 最美的花儿都是给人看 就如同你的靥 深埋在心底的弦 演奏着谁的幸福 拨弄心弦的双手 是否也在述说心底的悲凉 只是 ...
    卡卡西8788阅读 135评论 0 1
  • 上周六是我们暑假敬老院活动的最后一次,由我部卢元杰和赵琪带领一六护一的志愿者们到恒星敬老院完成了这次活动。 虽然是...
    青岛卫校志工部阅读 74评论 0 0