之前尝试过对 Laravel 的 Controller 做 Feature Test,但是在业务变得越来越复杂之后,我感觉对 controller 里的函数也没了自信,急需对功能函数做 Unit Test,以平复我不安的情绪。

例如:

新建一个 Unit Test,由于默认新建的是 feature test, 所有后面需要加上 unit 参数

php artisan make:test StaffSalaryHistoryTest --unit

此时会看到 tests/Unit 目录下多了一个文件

Untracked files:
(use "git add <file>..." to include in what will be committed) tests/Unit/StaffSalaryHistoryTest.php

测试代码

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Http\Controllers\Admin\StaffSalaryHistoryCrudController; class StaffSalaryHistoryTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function test_get_start_date_by_last_end_month()
{
$controller = new StaffSalaryHistoryCrudController();
$result = $controller->get_start_date_by_last_end_month('2018-07');
$this->assertEquals('2018-08-01', $result->format('Y-m-d'));
}
}

执行测试

 ./vendor/bin/phpunit
PHPUnit 6.5.12 by Sebastian Bergmann and contributors. ... 3 / 3 (100%) Time: 961 ms, Memory: 16.00MB OK (3 tests, 3 assertions)

对 Laravel 的 Controller 做 Unit Test的更多相关文章

  1. 使用Spring MockMVC对controller做单元测试(转)

    https://www.cnblogs.com/ylty/p/6420738.html 1.对单一controller做测试. import org.junit.Before; import org. ...

  2. 使用Spring MockMVC对controller做单元测试

    1.对单一controller做测试. import org.junit.Before; import org.junit.Test; import org.springframework.beans ...

  3. laravel整合workerman做聊天室

    测试工具  http://www.blue-zero.com/WebSocket/ 2018年8月6日17:28:24 <?php namespace App\Console\Commands; ...

  4. laravel使用JWT做API认证

    最近项目做API认证,最终技术选型决定使用JWT,项目框架使用的是laravel,laravel使用JWT有比较方便使用的开源包:jwt-auth.php 后端实现JWT认证方法 使用composer ...

  5. Laravel 控制器 Controller

    一.控制器存在的意义 路由可以分发请求:路由中还可以引入 html 页面:我们可以在 route/web.php 中搞定一切了:但是如果把业务逻辑都写入到路由中:那路由将庞大的难以维护:于是控制器就有 ...

  6. Laravel中如何做数据库迁移

    总的来说,做一次独立数据库迁移只需要三步,分别是创建迁移文件.修改迁移文件.运行迁移 1.创建数据库迁移文件php artisan make:migration create_articles_tab ...

  7. laravel整合workerman做消息推送系统

    官方建议分离 workerman和mvc框架的结合,我去,这不是有点脑缺氧吗? 大量的业务逻辑,去独立增加方法和类库在写一次,实际业务中是不现实和不实际的 gateway增加一些这方面的工作,但是我看 ...

  8. Laravel 使用 JWT 做 API 认证之tymon/jwt-auth 1.0.0-beta.1实践 - moell - SegmentFault

    安装 将"tymon/jwt-auth": "1.0.0-beta.1" 添加到 composer.json 中,执行 composer update Prov ...

  9. [Laravel] 05 - Controller

    大纲 PHP章节貌似有类似功能,回头看下. 请求 URL 一.URL 处理方法 请求使用的是:symfony/http-foundation组件 $_GET, $_POST, $_COOKIE, $_ ...

随机推荐

  1. search 重要文件路径 搜索【原】

    hosts文件路径 C:/WINDOWS/system32/drivers/etc/ tnsnames.ora文件路径 C:/oraclexe/app/oracle/product/11.2.0/se ...

  2. codeforces - 432D Prefixes and Suffixes (next数组)

    http://codeforces.com/problemset/problem/432/D 转自:https://blog.csdn.net/tc_to_top/article/details/38 ...

  3. bzoj千题计划306:bzoj2342: [Shoi2011]双倍回文 (回文自动机)

    https://www.lydsy.com/JudgeOnline/problem.php?id=2342 解法一: 对原串构建回文自动机 抽离fail树,从根开始dfs 设len[x]表示节点x表示 ...

  4. datetimepicker bootstrap的时间插件显示位置问题及其他配置

    位置问题: 在bootstrap中用 datetimepicker 时默认是在输入框下面弹出的, 但是遇到输入框在屏幕最下面时,日期选择框会有一部分在屏幕下面,显示不了,因此需要能够从上面弹出. 可以 ...

  5. 六道JavaScript测验题

    1.找出数字数组中最大的元素(使用Match.max函数) var a=[123,23432,345,3,34]; console.log(Math.max.apply(null,a)); 2.转化一 ...

  6. u-boot移植(十二)---代码修改---支持DM9000网卡

    一.准备工作 1.1 原理图 CONFIG_DM9000_BASE 片选信号是接在nGCS4引脚,若要确定网卡的基地址,则要根据片选信号的接口去确定. 在三星2440的DATASHEET中memory ...

  7. 解决virtualbox与mac文件拖拽问题

    apt-get purge virtualbox-guest-x11apt-get autoremove --purgerebootapt-get updateapt-get dist-upgrade ...

  8. FCN 32分析:

    FCN 32s

  9. android SQLiteOpenHelper 使用

    1.实体 package mydemo.mycom.demo2.entity; public class UserInfo { private int id; private String usern ...

  10. C# 一般处理程序下载文件

    using System; using System.Collections; using System.Data; using System.Linq; using System.Web; usin ...