Angular 方法内部测试分支
Angular test branches inside method
我的TS文件里有这个方法,
里面有3个分支。
在 angular 中使用 jasmine,我如何测试所有这些分支?
getAges(ages: Ages) {
if (ages) {
return ages.number ? 10 : 20;
}
return 30;
}
这样你就可以完成所有三种路径:
class TestClass {
getAges(ages: any) {
if (ages) {
return ages.number ? 10 : 20;
}
return 30;
}
}
describe('getAges', () => {
const subject: TestClass = new TestClass();
it('when no ages are provided', () => {
expect(subject.getAges(null)).toBe(30);
});
it('ages has a value', () => {
const ages = { number: 5 };
expect(subject.getAges(ages)).toBe(10);
});
it('ages does not have a value', () => {
const ages = {};
expect(subject.getAges(ages)).toBe(20);
});
});
此处以TestClass为例
我的TS文件里有这个方法,
里面有3个分支。
在 angular 中使用 jasmine,我如何测试所有这些分支?
getAges(ages: Ages) {
if (ages) {
return ages.number ? 10 : 20;
}
return 30;
}
这样你就可以完成所有三种路径:
class TestClass {
getAges(ages: any) {
if (ages) {
return ages.number ? 10 : 20;
}
return 30;
}
}
describe('getAges', () => {
const subject: TestClass = new TestClass();
it('when no ages are provided', () => {
expect(subject.getAges(null)).toBe(30);
});
it('ages has a value', () => {
const ages = { number: 5 };
expect(subject.getAges(ages)).toBe(10);
});
it('ages does not have a value', () => {
const ages = {};
expect(subject.getAges(ages)).toBe(20);
});
});
此处以TestClass为例