对 Laravel 的 Controller 做 Unit Test
之前尝试过对 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的更多相关文章
- 使用Spring MockMVC对controller做单元测试(转)
https://www.cnblogs.com/ylty/p/6420738.html 1.对单一controller做测试. import org.junit.Before; import org. ...
- 使用Spring MockMVC对controller做单元测试
1.对单一controller做测试. import org.junit.Before; import org.junit.Test; import org.springframework.beans ...
- laravel整合workerman做聊天室
测试工具 http://www.blue-zero.com/WebSocket/ 2018年8月6日17:28:24 <?php namespace App\Console\Commands; ...
- laravel使用JWT做API认证
最近项目做API认证,最终技术选型决定使用JWT,项目框架使用的是laravel,laravel使用JWT有比较方便使用的开源包:jwt-auth.php 后端实现JWT认证方法 使用composer ...
- Laravel 控制器 Controller
一.控制器存在的意义 路由可以分发请求:路由中还可以引入 html 页面:我们可以在 route/web.php 中搞定一切了:但是如果把业务逻辑都写入到路由中:那路由将庞大的难以维护:于是控制器就有 ...
- Laravel中如何做数据库迁移
总的来说,做一次独立数据库迁移只需要三步,分别是创建迁移文件.修改迁移文件.运行迁移 1.创建数据库迁移文件php artisan make:migration create_articles_tab ...
- laravel整合workerman做消息推送系统
官方建议分离 workerman和mvc框架的结合,我去,这不是有点脑缺氧吗? 大量的业务逻辑,去独立增加方法和类库在写一次,实际业务中是不现实和不实际的 gateway增加一些这方面的工作,但是我看 ...
- 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 ...
- [Laravel] 05 - Controller
大纲 PHP章节貌似有类似功能,回头看下. 请求 URL 一.URL 处理方法 请求使用的是:symfony/http-foundation组件 $_GET, $_POST, $_COOKIE, $_ ...
随机推荐
- Docker mysql主主互备和高可用
一.测试环境 1.1 结构图 1.2 版本 操作系统:CentOS 7.3 MySQL版本:5.6.35 Docker版本:18.06.1-ce 使用root用户操作 IP地址说明 IP地址 用途 备 ...
- django 403问题
C:\Users\x\pyp1>python manage.py runserverPerforming system checks... System check identified no ...
- secure CRT the remote system refused the connection 解决办法
1.安装ssh服务器和客户端 apt-get install openssh-server apt-get install openssh-client 2.重启ssh /etc/init.d/ssh ...
- Vue.js 开发环境的搭建
1. cnpm install vue-cli(安装vue 脚手架) 2. vue init webpack my-project(my-project:自定义,取一个项目的名字,init 初始化一个 ...
- JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮【转】
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...
- 浅析 Bigtable 和 LevelDB 的实现
在 2006 年的 OSDI 上,Google 发布了名为 Bigtable: A Distributed Storage System for Structured Data 的论文,其中描述了一个 ...
- contourf和contour用法区别
import numpy as np import matplotlib.pyplot as plt %matplotlib inline from matplotlib.colors import ...
- 【C++ STL】Set和Multiset
1.结构 set和multiset会根据特定的排序原则将元素排序.两者不同之处在于,multisets允许元素重复,而set不允许重复. 只要是assignable.copyable.comparab ...
- jquery 兼容的滚轮事件
// jquery 兼容的滚轮事件 $(document).on("mousewheel DOMMouseScroll", function (e) { ? : -)) || // ...
- C#窗口防止闪烁两种方法
public class PaintIncrease { public static void SetDoubleBuffered(object obj) { Type type = obj.GetT ...