之前尝试过对 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. Docker:使用Jenkins构建Docker镜像

    Docker  彭东稳  1年前 (2016-12-27)  10709次浏览  已收录  0个评论 一.介绍Jenkins Jenkins是一个开源项目,提供了一种易于使用的持续集成系统,使开发者从 ...

  2. java代码示例(4)

    /** * 需求分析:计算100以内的和,用while * @author chenyanlong * 日期:2017/10/14 */ package com.hp.test04; public c ...

  3. H5新特性之拖拽文件

    H5新增了drag事件,在H5中拖拽是十分常见的. 可以拖拽的分为页面内的和页面外的 页面内的一般默认可以拖拽的是img和a标签 页面外的常指的是文件 上代码吧~ let zoom = documen ...

  4. ansible指路篇-安装及基本命令使用

    ansible指路篇-安装及基本命令使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是ansible ansible是新出现的自动化运维工具,基于Python开发,集合 ...

  5. 一张非常强大的OSI七层模型图解。。。

    源自http://www.colasoft.com.cn/download/protocols_map.php,非常适合小白入门,后面罗列出来方便大家浏览记忆...(不经意间看到的,分享一下) OSI ...

  6. java compareTo() 用法注意点

    转自:http://www.2cto.com/kf/201305/210466.html compareTo就是比较两个值,如果前者大于后者,返回1,等于返回0,小于返回-1,我下面给出了例子,由于比 ...

  7. spyder里的"查找文件里的特定字符串"非常方便

    spyder里的"查找文件里的特定字符串"非常方便 快捷键是: ^+shift+F 使用方法: 输入被查找的字符串: 比如: .rcParam, 注意大小写敏感的(默认设置) 回车 ...

  8. spring boot(九):Spring Boot中Redis的使用

    Redis实战代码 1.引入 spring-boot-starter-redis <dependency> <groupId>org.springframework.boot& ...

  9. Linux 重启网卡失败 Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.

    linux下重启网卡使用命令 : service network restart 时报错: [root@slave01 hadoop]# service network restart Startin ...

  10. <转载>iTerm2使用技巧

    原文链接:http://www.cnblogs.com/756623607-zhang/p/7071281.html   1.设置窗口 定位到 [Preferences - Profiles - Wi ...