在先后台开发的项目中,咱们当前已经作到了后台依靠单元测试,彻底的脱离前台进行开发。那么,在进行单台开发时,是否也能够作到只依赖于UML
图,不依赖于后台进行独立的开发呢?答案是确定的。css
本文旨在带领你:在前台彻底脱离后台开发路上更近一步。html
在此,咱们以近期开发的Alice学生管理系统为例,将学院管理的index
组件拿出来,以展现脱离后台的前台。
文件列表:跨域
CollegeIndexComponent: 学院管理INDEX列表组件 College: 学院实体 CollegeService: 学院服务 其它辅助文件略
CollegeIndexComponent 学院管理INDEX列表组件浏览器
import {Component, OnInit} from '@angular/core'; import {CollegeService} from '../../../../../service/college.service'; import {College} from '../../../../../entity/college'; import {Page} from '../../../../../entity/page'; @Component({ selector: 'app-college-index', templateUrl: './college-index.component.html', styleUrls: ['./college-index.component.css'] }) export class CollegeIndexComponent implements OnInit { page: number; size: number; total: number; collegeList: Array<College>; constructor(private collegeService: CollegeService) { } ngOnInit() { console.log(this.collegeService); this.collegeList = new Array<College>(); this.page = 1; this.size = 5; this.queryPage(); } queryPage() { this.collegeService.getCollegeByPage(this.page - 1, this.size) .subscribe((data: Page<College>) => { this.collegeList = data.content; this.total = data.totalElements; }, () => { console.log('network error'); }); } }
College: 学院实体antd
import {Teacher} from './teacher'; import {Major} from './major'; /** * 学院实体 */ export class College { id: number; // id name: string; // 名称 teacherList: Teacher[]; // 负责教师 majorList: Major[]; [propName: string]: any; /** * 获取学院实体 */ static instance(): College { const college = new College(); college.id = 1; college.name = '测试学院'; college.teacherList = new Array<Teacher>( Teacher.instance() ); college.majorList = new Array<Major>(); return college; } }
CollegeService: 学院服务app
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { College } from '../entity/college'; import { Observable } from 'rxjs'; import { Page } from '../entity/page'; @Injectable({ providedIn: 'root', }) export class CollegeService { baseUrl = 'College'; constructor(protected http: HttpClient) {} /** *获取分页数据 */ public getCollegeByPage(page: number, size: number): Observable<Page<College>> { const params = { page: page.toString(), size: size.toString(), }; return this.http.get<Page<College>>(this.baseUrl + '/page', { params: params, }); } }
代码以下:async
import {async, ComponentFixture, TestBed} from '@angular/core/testing'; import {CollegeIndexComponent} from './college-index.component'; import {NzGridModule, NzDividerModule, NzTableModule, NzFormModule} from 'ng-zorro-antd'; import {HttpClientModule} from '@angular/common/http'; import {RouterTestingModule} from '@angular/router/testing'; import {FormsModule, ReactiveFormsModule} from '@angular/forms'; /** * 对测试的描述:CollegeIndexComponent */ describe('CollegeIndexComponent', () => { // 定义两个变量 let component: CollegeIndexComponent; let fixture: ComponentFixture<CollegeIndexComponent>; beforeEach(async(() => { // TestBed工做台 // TestBed.configureTestingModule 配置要测试的模块 // 这贴近于现实生活。现实生活中,咱们测试一块电表是否正确. // 1. 须要一个专门用于测试的工做台 // 2. 须要对这个测试的工做进行配置,以让其符合测试电表的基础工做 TestBed.configureTestingModule({ // 声明要上工做台的组件 declarations: [CollegeIndexComponent], // 配置测试的依赖,没有这些模块,测试就进行不了。 // 好比咱们测试电表是否准确,须要有交流电,须要有电流表,须要有电压表等 imports: [NzGridModule, HttpClientModule, NzDividerModule, NzTableModule, RouterTestingModule, ReactiveFormsModule, FormsModule, NzFormModule] }).compileComponents(); // 配置完后,开始编译要测试组件 })); // 每次测试前,均执行一遍 beforeEach(() => { // fix = 固定。用工做台建立一个供测试的组件。 // 因为其想要被测试,就必然还须要其它的一些脚手架。 // 咱们把脚手架与被测试的组件组成的联合体称为:ComponentFixture 被固定的组件 fixture = TestBed.createComponent(CollegeIndexComponent); // 实例化要测试的组件 component = fixture.componentInstance; // 检测变化 fixture.detectChanges(); }); /** * 测试方法的描述信息:should create */ it('should create', () => { // 期待 组件 被成功建立 expect(component).toBeTruthy(); }); });
执行此单元测试,虽然可以经过,但将在控制台获得如下错误信息。ide
同时,展现的界面以下:单元测试
咱们看到,在测试时依赖于httpClient
发起的请求,因为咱们没有启动后台服务(即便启动了,使用绝对地址访问的方法也有问题),因此没法由后台获取数据。其实,即便是咱们进行跨域的地址设置,因为后台咱们每每会进行权限验证(是否登陆,当前登陆用户是否有当前操做的权限等),因此:想发起一个正常的请求,并不容易。测试
下面,让咱们在不破坏原有服务层的基础上,自定义返回数据,从而规避http
请求,达到不须要后台,即可以看到真实数据的目的。
在单元测试时,调用beforeEach
及it
方法时都可以将@angular/core/testing -> injetct()
传入。在此,咱们以beforeEach
为例,注入CollegeService
,并在测试组件实例化之前,对其getCollegeByPage
方法的返回值,进行重写。
/** * 每次测试前,均执行一遍 * inject([], () => {}) 单元测试中的方法 * CollegeService 要注入的服务 * StudentService 要注入的服务 (仅用于展现注入多个服务) * s为服务起的别名,类型为CollegeService,其对应注入的第一个参数 * t为服务起的别名,类型为StudentService,其对应注入的第二个参数(仅用于展现注入多个服务) */ beforeEach(inject([CollegeService, StudentService], (s: CollegeService, t: StudentService) => { console.log(s); console.log(t); // fix = 固定。用工做台建立一个供测试的组件。 // 因为其想要被测试,就必然还须要其它的一些脚手架。 // 咱们把脚手架与被测试的组件组成的联合体称为:ComponentFixture 被固定的组件 fixture = TestBed.createComponent(CollegeIndexComponent); // 实例化要测试的组件 component = fixture.componentInstance; // 检测变化 fixture.detectChanges(); }));
使用以下方法改写返回值:
/** * 每次测试前,均执行一遍 * inject([], () => {}) 单元测试中的方法 * CollegeService 要注入的服务 * StudentService 要注入的服务(仅用于展现注入多个服务) * collegeService为服务起的别名,类型为CollegeService,其对应注入的第一个参数 * studentService为服务起的别名,类型为StudentService,其对应注入的第二个参数(仅用于展现注入多个服务) */ beforeEach(inject([CollegeService, StudentService], (collegeService: CollegeService, studentService: StudentService) => { // 变量初始化 const collegePage = new Page<College>(); collegePage.content = new Array<College>( College.instance() ); // 改写collegeService中的getCollegeByPage方法的返回值。 spyOn(collegeService, 'getCollegeByPage').and.returnValue(of(collegePage)); // fix = 固定。用工做台建立一个供测试的组件。 // 因为其想要被测试,就必然还须要其它的一些脚手架。 // 咱们把脚手架与被测试的组件组成的联合体称为:ComponentFixture 被固定的组件 fixture = TestBed.createComponent(CollegeIndexComponent); // 实例化要测试的组件 component = fixture.componentInstance; // 检测变化 fixture.detectChanges(); }));
此时,咱们再次测试,正常的经过了测试,控制台没有报错,并且:咱们在浏览器上看到了咱们的组件。最关键的是:咱们即没有使用后台,也没有改变CollegeService
中的代码。
参考文献:
深度好文