我是第一次玩笑。所以我想测试这个方法:
public static getProjectBranch(toto: any): string {
if ("branch" in toto) {
return toto.branch;
} else {
return "master";
}
}
此方法在totoService.ts类中
我在totoService.spec.ts中正在做什么:
describe("Test get Project Branch", () => {
test("branch is in component", () => expect(getProjectBranch()).toBe(""));
});
我想知道我在做什么是好还是没有以及如何导入文件中的getProjectBranch方法?
由于您的方法getProjectBranch
是静态的,因此您可以像下面显示的那样简单地做:
describe("TotoService",() => {
describe('getProjectBranch', () => {
test("branch is in component",() => {
const toto = {branch:''}; //create your test object here
expect(totoService.getProjectBranch(toto)).toEqual(''); //call static method of TotoService
})
})
})
如果您要调用非静态方法,则需要创建totoService beforeEach
测试的实例:
describe("TotoService",() => {
let totoService;
beforeEach(() => {
totoService = new TotoService();
})
describe('getProjectBranch', () => {
test("branch is in component",() => {
const toto = {branch:''};
expect(totoService.getProjectBranch(toto)).toEqual('');
})
})
})