安装相关插件
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]
网友评论