前言
之前对单元测试的认知就是复杂,难搞,思路有,就是不知道怎样去实现,最近一次开会解决问题的过程中,发现原来单元测试可以十分简单,简单到几行代码就能实现。
示例
下面代码实现的功能是,判断课程所在的学院<College> college是否在用户所有的学院Array<College> colleges中,如果存在,变量show赋值为true,不存在,则赋值为false,如果college为undefined或者null,也赋值为true。
/** * 观察课程学院是否与用户所在学院相同 * @param college 课程学院 * @param colleges 用户学院 */ public whetherShow(college: { id: number }, colleges: Array<{ id: number }>) { Assert.notNull(college, 'college未定义'); const collegeId = college.id; let show = colleges != null && colleges && colleges.length > 0 "htmlcode">it('is show', () => { const course = new Course({id: 1}) const collegeOne = new College({id: 1}); const collegeTwo = new College({id: 2}); component.colleges = []; expect(component.whetherShow(course,component.colleges)).toBe(true); component.colleges = undefined; expect(component.whetherShow(course,component.colleges)).toBe(true); component.colleges = [collegeOne]; expect(component.whetherShow(course,component.colleges)).toBe(true); component.colleges = [collegeTwo]; expect(component.whetherShow(course,component.colleges)).toBe(false); component.colleges = [collegeOne, collegeTwo]; expect(component.whetherShow(course,component.colleges)).toBe(true); });通过控制台的信息可以发现,无论是null还是undefined,都是可以通过的,后来老师提供了新的思路,既然要测试的是功能,就不要管怎么传的,可以不用传对象,然后就有了下面的写法:
it('is show', () => { expect(component.whetherShow({id: 1}, null)).toBe(true); expect(component.whetherShow({id: 1}, undefined)).toBe(true); expect(component.whetherShow({id: 1}, [])).toBe(true); expect(component.whetherShow({id: 1}, [{id: 2}, {id: 3}])).toBe(false); expect(component.whetherShow({id: 1}, [{id: 1}, {id: 2}, {id: 3}])).toBe(true); expect(component.whetherShow({id: 1}, [{id: 2}, {id: 3}, {id: 1}])).toBe(true); });值传进去了,方法也能判断了,比起之前的写法简直要好太多,而且对于一些方法来说,这种方法省力不少,尤其是对多种情况进行测试,要进行多个变量的定义:
/** * 判断查询的关键字是否课程代码或名称中 * @param course 课程 * @param searchKey 查询关键字 */ public isCodeOrNameContainsSearchKey(course: { code: string, name: string }, searchKey: string) { return searchKey === null || course.code.toLowerCase().indexOf(searchKey.toLowerCase()) !== -1 || course.name.toLowerCase().indexOf(searchKey.toLowerCase()) !== -1; }该方法实现的是通过课程名称或代码进行查询操作,通过对查询关键字和课程名称或代码进行对比实现该功能,要考虑以下几种情况:查询关键字为null、查询关键字与课程名称或代码部分完全不相同、查询关键字与课程名称或代码部分相同、查询关键字与课程名称或代码完全相同、查询关键字包含课程名称或代码。
如果用旧思想进行测试:
it('isCodeOrNameContainsSearchKey', () => { const courseOne = new Course({code: '', name: ''}); const courseTwo = new Course({code: '222', name: ''}); const courseThree = new Course({code: '', name: '222'}); const courseFour = new Course({code: '222', name: '222'}); expect(component.isCodeOrNameContainsSearchKey(courseOne, null)); expect(component.isCodeOrNameContainsSearchKey(courseOne, '')); expect(component.isCodeOrNameContainsSearchKey(courseTwo, '')); expect(component.isCodeOrNameContainsSearchKey(courseTwo, '1111')); expect(component.isCodeOrNameContainsSearchKey(courseTwo, '22')); expect(component.isCodeOrNameContainsSearchKey(courseTwo, '222')); expect(component.isCodeOrNameContainsSearchKey(courseTwo, '2222')); expect(component.isCodeOrNameContainsSearchKey(courseThree, '')); expect(component.isCodeOrNameContainsSearchKey(courseThree, '1111')); expect(component.isCodeOrNameContainsSearchKey(courseThree, '22')); expect(component.isCodeOrNameContainsSearchKey(courseThree, '222')); expect(component.isCodeOrNameContainsSearchKey(courseThree, '2222')); expect(component.isCodeOrNameContainsSearchKey(courseFour, '')); expect(component.isCodeOrNameContainsSearchKey(courseFour, '1111')); expect(component.isCodeOrNameContainsSearchKey(courseFour, '22')); expect(component.isCodeOrNameContainsSearchKey(courseFour, '222')); expect(component.isCodeOrNameContainsSearchKey(courseFour, '2222')); });如果使用新思想:
it('isCodeOrNameContainsSearchKey', () => { expect(component.isCodeOrNameContainsSearchKey({code: '', name: ''}, null)).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '', name: ''}, '')).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '222', name: ''}, '')).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '222', name: ''}, '1111')).toBe(false); expect(component.isCodeOrNameContainsSearchKey({code: '222', name: ''}, '22')).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '222', name: ''}, '222')).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '222', name: ''}, '2222')).toBe(false); expect(component.isCodeOrNameContainsSearchKey({code: '', name: '222'}, '')).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '', name: '222'}, '1111')).toBe(false); expect(component.isCodeOrNameContainsSearchKey({code: '', name: '222'}, '22')).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '', name: '222'}, '222')).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '', name: '222'}, '2222')).toBe(false); expect(component.isCodeOrNameContainsSearchKey({code: '222', name: '222'}, '')).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '222', name: '222'}, '1111')).toBe(false); expect(component.isCodeOrNameContainsSearchKey({code: '222', name: '222'}, '22')).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '222', name: '222'}, '222')).toBe(true); expect(component.isCodeOrNameContainsSearchKey({code: '222', name: '222'}, '2222')).toBe(false); });设想一下自己看到他人写的测试代码,如果所需要的变量很少,courseOne等等能满足需求,看着也没问题,但是当变量很多的时候,估计写测试的都会忘记每个变量的属性值,更不用说看的人,而且,使用下面的方法写的代码,所需字段以及字段值一目了然,一行代码就能体现所有信息,看着也赏心悦目。
总结
简单的单元测试写起来真的要简单很多,而且感觉比之前的要优雅很多,看起来真的挺整洁的,整整齐齐的看着很舒服,感谢潘老师的指导,也感谢小伙伴们给予的帮助。
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 黑鸭子2008-男人女人[首版][WAV+CUE]
- 张佳佳 《FOLK SONG Ⅱ Impromptus OP.23(即兴曲7首)》[320K/MP3][98.71MB]
- 祖海 《我家在中国 (维也纳金色大厅独唱音乐会)》[320K/MP3][118.55MB]
- 祖海 《我家在中国 (维也纳金色大厅独唱音乐会)》[FLAC/分轨][268.08MB]
- 张信哲.1996-思念【EMI百代】【WAV+CUE】
- 江美琪.2024-圆的?圆的>华纳】【FLAC分轨】
- 许巍.2018-无尽光芒【和雅弘嘉】【WAV+CUE】
- 庆怜 CAELAN《THE HALF-BLOOD PRINCE 半血王子》[320K/MP3][65.72MB]
- 庆怜 CAELAN《THE HALF-BLOOD PRINCE 半血王子》[FLAC/分轨][378.53MB]
- Fine乐团《废墟游乐》[320K/MP3][105.13MB]
- 万山红.2009-花开原野万山红Vol.1-2【柏菲】2CD【WAV+CUE】
- 曾庆瑜1992-18首中英文经典全集[台湾派森][WAV整轨]
- 【上扬爱乐】群星-TheSoundsofLS35AVol.4情迷4【低速原抓WAV分轨】
- Fine乐团《废墟游乐》[Hi-Res][24bit 48kHz][FLAC/分轨][767.04MB]
- Cicada《回返 (十五周年自选集)》[320K/MP3][93.87MB]