Laravel 单元测试那些事(一)
最近看了论坛里的大神教程,自己也在自己的项目中实践TDD,以下是个人在学习过程中总结的一些知识点,希望给需要的朋友带来帮助!
TDD 开发的基本原则
- 在编写失败的测试之前,不要编写任何业务代码;
- 只要有一个单元测试失败了,就不要再些测试代码。无法通过编译也是一种失败情况;
- 业务代码恰好能够让当前失败的测试成功通过即可,不要多写;
- 一般而言,当修改已通过的测试时,应该在修改之后(需注释新建的测试)再次测试,确保之前的测试逻辑未被破坏。
常用命令
新建一个单元测试
php artisan make:test ReplyTest --unit
单独执行某个测试
phpunit tests/Unit/ReplyTest.php
单独执行某个测试中的函数
phpunit --filter a_thread_has_a_creator
带上测试环境变量进行测试就不需要 CsrfToken
APP_ENV=testing phpunit --filter an_authenticated_user_may_participate_in_forum_threads
APP_ENV=testing phpunit
一些技巧
更好的查看测试异常信息
在 app\Exceptions\Handler.php 中加上一行:
public function render($request, Exception $exception)
{
if(app()->environment() === 'local') throw $exception; // 此处加上一行
// 注意,只有环境变量是 local 的时候才会加上这行,是不是也可以设定为 testing,教程上说不起作用,但是带上测试环境变量之后就可以使用 testing 啦!
return parent::render($request, $exception);
}
模型create()与make()方法的区别
/**
* Create a collection of models and persist them to the database.
*
* @param array $attributes
* @return mixed
*/
public function create(array $attributes = [])
{
$results = $this->make($attributes);
if ($results instanceof Model) {
$this->store(collect([$results]));
} else {
$this->store($results);
}
return $results;
}
/**
* Create a collection of models.
*
* @param array $attributes
* @return mixed
*/
public function make(array $attributes = [])
{
if ($this->amount === null) {
return $this->makeInstance($attributes);
}
if ($this->amount < 1) {
return (new $this->class)->newCollection();
}
return (new $this->class)->newCollection(array_map(function () use ($attributes) {
return $this->makeInstance($attributes);
}, range(1, $this->amount)));
}
create()
方法会得到一个实例,并将实例保存到数据库中;make()
方法只会得到一个实例。在本节的测试中我们不需要保存$thread实例,因为我们会在RepliesController
的store()
方法进行保存,故使用make()方法。
简单的说,当我们在编写测试用例的时候,如果是模拟用户填写表单的数据,那就不需要写入数据库中,也就是说,不需要使用create()
方法,而只需要使用make()
方法来创建即可。
- create()方法得到一个模型实例,并保存到数据库中;
- make()方法得到一个模型实例(不保存);
- raw()方法是得到一个模型实例转化后的数组。
创建便于测试的辅助函数
修改composer.json
并导出自动加载文件
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
},
"files":["tests/utilities/functions.php"] -->这里增加一行
},
composer dump-autoload
将几个常用的函数封装到functions.php
中去:
<?php
function create($class,$attributes = [])
{
return factory($class)->create($attributes);
}
function make($class,$attributes = [])
{
return factory($class)->make($attributes);
}
function raw($class,$attributes = [])
{
return factory($class)->raw($attributes);
}
在很多测试中,我们需要测试用户是否登录。在之前的测试当中,我们使用了be(),actingAs()方法来得到一个已登录用户。现在我们在TestCase.php新建signIn()方法,将用户登录的逻辑放在基类文件中:
\tests\TestCase.php
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function signIn($user = null)
{
$user = $user ?: create('App\Models\User');
$this->actingAs($user);
return $this;
}
}
当然,我们可以使用编辑器来创建 Snippet 这样,我们就可以快速的构建自己的代码片断:
VS Code 自定义 Snippet 教程
常用断言
断言:判断 Model 得到的结果是某个类的实例 assertInstanceOf()
/** @test */
public function a_thread_has_replies()
{
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection',$this->thread->replies);
}
/** @test */
public function a_thread_has_a_creator()
{
$this->assertInstanceOf('App\User',$this->thread->creator);
}
断言:带用户授权和不带用户授权的写法
/** @test */
public function unauthenticated_user_may_no_add_replies()
{
$this->expectException('Illuminate\Auth\AuthenticationException');
$thread = factory('App\Thread')->create();
$reply = factory('App\Reply')->create();
$this->post($thread->path().'/replies',$reply->toArray());
}
/** @test */
function an_authenticated_user_may_participate_in_forum_threads()
{
// Given we have a authenticated user
$this->be($user = factory('App\User')->create());
// And an existing thread
$thread = factory('App\Thread')->create();
// When the user adds a reply to the thread
$reply = factory('App\Reply')->create();
$this->post($thread->path() .'/replies',$reply->toArray());
// Then their reply should be visible on the page
$this->get($thread->path())
->assertSee($reply->body);
}
注意:带用户授权的时候使用 $this->be()
方法,不带用户授权的时候使用:expectException
断言
创建已登录用户的方法 actingAs
/** @test */
public function an_authenticated_user_can_create_new_forum_threads()
{
// Given we have a signed in user
$this->actingAs(factory('App\User')->create()); // 已登录用户
// When we hit the endpoint to cteate a new thread
$thread = factory('App\Thread')->make();
$this->post('/threads',$thread->toArray());
// Then,when we visit the thread
// We should see the new thread
$this->get($thread->path())
->assertSee($thread->title)
->assertSee($thread->body);
}
/** @test */
public function guests_may_not_create_threads()
{
$thread = factory('App\Thread')->make();
$this->post('/threads',$thread->toArray());
}
class ThreadsController extends Controller
{
public function __construct()
{
$this->middleware('auth')->only('store'); // 白名单,意味着仅 store 方法需要登录
//这么做的意思是用户登录未登录时不可以创建帖子,并且会正常的抛出未授权异常,而不是数据库异常
}
好啦,今天就写到这里,后面继续更新
本作品采用《CC 协议》,转载必须注明作者和本文链接
请问后面还有吗