我刚开始为Angular 6编写测试用例,这是我的服务代码。如何为开关盒编写测试用例。我不知道怎么写。
import { Injectable } from '@angular/core';
import { HttpHeaders } from '@angular/common/http';
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' })};
const CUSTOMERS_URL = "http://localhost:8009/api/customers";
import * as sample1 from "./schema/schema-sample1.json";
import * as sample2 from "./schema/schema-sample2.json";
import * as sample3 from "./schema/schema-sample3.json";
import * as sample4 from "./schema/schema-sample4.json";
import * as sample5 from "./schema/schema-sample5.json";
@Injectable({
providedIn: 'root'
})
export class AppService {
constructor() { }
getDynamicRequestDetailsForApp(appName){
switch(appName) {
case "dcc": {
return sample1;
break;
}
case "sbr": {
return sample2;
break;
}
case "arc": {
return sample3;
break;
}
case "auth": {
return sample5;
break;
}
default: {
return sample5;
break;
}
}
}
}
答案 0 :(得分:0)
基本上switch
是类似于if,else if, else
的语句。您可以将case
假定为else if
,将default
假定为else
。对于上面的代码示例测试可能如下所示
describe('Service: FormService', () => {
beforeEach(() => {
service = new AppService();
});
it('tests sbr', () => {
expect(service.getDynamicRequestDetailsForApp('sbr')).toEqual(sample2);
});
it('tests dcc', () => {
expect(service.getDynamicRequestDetailsForApp('dcc')).toEqual(sample1);
});
/* Same for remaining case statements with different function parameter */
});
答案 1 :(得分:0)
这是一个 Jasmin 单元测试模型,当我们要测试组件的Method是否具有switch()
条件时,一旦触发,实际上就在{ {1}}最后一个分支。
这样,我们将在单元测试覆盖率表的所有列上达到 100%-在default
,Statements
,{{1} }和Branches
Functions
该组件的Lines
是:
describe(`'onTodoManipulation()' method: should`, () => {
...
it()
...
it(`console.error(), if arg.action !== 'update' && !== 'delete'` , () => {
// --------------------------------------------------
// Prepare it - it wasn't that easy!
// --------------------------------------------------
interface TodoObjToEdit {
action: string;
item: ItemWithId;
propToUpdate: { [key: string]: string | boolean };
}
// tslint:disable-next-line: max-line-length
const commonTodoItem: ItemWithId = { todo: 'TODO text', done: false, userId: 'someUserId123', autoID: 'I didn\'t made it!' };
//
//
// 1) mock an arg, for thisComponent's 'onTodoManipulation()' Method,
// where 'someDoneTodoUpdate.action' is NOT 'update', nor 'delete':
const someDoneTodoUpdate: TodoObjToEdit = {
action: 'this action is NOT "update" and is NOT "delete"',
item: commonTodoItem,
propToUpdate: { done: true}
};
// tslint:disable-next-line: max-line-length
// 2) mock a 'mockedErrorFn' console.error, that will be triggered on the thisComponent's 'onTodoManipulation()' Method,
// if, and only if, 'someDoneTodoUpdate.action' is NOT 'update', nor 'delete'
let consoleOutput: string;
const mockedErrorFn: {
(...data: any[]): void;
(message?: any, ...optionalParams: any[]): void;
} = (output: string) => consoleOutput = output;
console.error = mockedErrorFn;
// --------------------------------------------------
// Now we can trigger the thisComponent's Method we want to expect() something - a console.error trigger:
thisComponent.onTodoManipulation(someDoneTodoUpdate);
expect(consoleOutput).toContain('Please code it, first!');
// and to reinforce this expect() is totally legit:
expect(consoleOutput).not.toContain('Please codeXXX it, first!');
});
});
我希望这会有所帮助。
抱歉,有些冗长,但是通过在我简单的 Angular-Firebase TS file
上复制并粘贴我所做的事情,我并没有花费太多时间...
享受!