Angular 如何提高茉莉花的覆盖率
我对茉莉花和因果报应一无所知。组件法在提高覆盖率方面面临的问题 如何增加订阅块中if-else条件的覆盖率 我在app.component.ts文件中调用了a服务方法。我必须验证响应并根据结果显示图像,但覆盖率没有得到覆盖 应用程序组件.tsAngular 如何提高茉莉花的覆盖率,angular,typescript,jasmine,karma-jasmine,Angular,Typescript,Jasmine,Karma Jasmine,我对茉莉花和因果报应一无所知。组件法在提高覆盖率方面面临的问题 如何增加订阅块中if-else条件的覆盖率 我在app.component.ts文件中调用了a服务方法。我必须验证响应并根据结果显示图像,但覆盖率没有得到覆盖 应用程序组件.ts export class AppComponent { title = 'bank'; flag: string; image: string; private regex: RegExp = new RegExp(/-(.*?)-/);
export class AppComponent {
title = 'bank';
flag: string;
image: string;
private regex: RegExp = new RegExp(/-(.*?)-/);
constructor(private service : FlagService) {}
ngOnit() {
this.service.getFlag().subscribe(
(res) => {
this.flag = res.FlagModel[0].code.match(this.regex)[1];
if(this.flag === 'dev') {
this.image = '../assessts/icons/dev.png'
} else if(this.flag === 'qa') {
this.image = '../assessts/icons/qa.png'
} else if(this.flag === 'prod') {
this.image = '../assessts/icons/prod.png'
}
}, (error) =>{
console.log(error);
});
}
}
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let flagServiceTest: FlagService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [
AppComponent
],
providers: [
HttpClient,
HttpHandler,
FlagService
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
flagServiceTest = TestBed.get(FlagService);
fixture.detectChanges();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it(`should have as title 'bank'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('bank');
});
it('should call subscribe method', fakeAsync(() => {
const flagSpy = spyOn(flagServiceTest,'getFlag').and.callThrough();
fixture.detectChanges();
component.ngOnit();
tick();
expect(flagServiceTest.getFlag()).toHaveBeenCalled();
expect(flagSpy).toHaveBeenCalled();
expect(flagSpy.calls.any()).toBeTruthy();
}));
});
app.component.specs.ts
export class AppComponent {
title = 'bank';
flag: string;
image: string;
private regex: RegExp = new RegExp(/-(.*?)-/);
constructor(private service : FlagService) {}
ngOnit() {
this.service.getFlag().subscribe(
(res) => {
this.flag = res.FlagModel[0].code.match(this.regex)[1];
if(this.flag === 'dev') {
this.image = '../assessts/icons/dev.png'
} else if(this.flag === 'qa') {
this.image = '../assessts/icons/qa.png'
} else if(this.flag === 'prod') {
this.image = '../assessts/icons/prod.png'
}
}, (error) =>{
console.log(error);
});
}
}
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let flagServiceTest: FlagService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [
AppComponent
],
providers: [
HttpClient,
HttpHandler,
FlagService
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
flagServiceTest = TestBed.get(FlagService);
fixture.detectChanges();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it(`should have as title 'bank'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('bank');
});
it('should call subscribe method', fakeAsync(() => {
const flagSpy = spyOn(flagServiceTest,'getFlag').and.callThrough();
fixture.detectChanges();
component.ngOnit();
tick();
expect(flagServiceTest.getFlag()).toHaveBeenCalled();
expect(flagSpy).toHaveBeenCalled();
expect(flagSpy.calls.any()).toBeTruthy();
}));
});
- 您需要为每个if语句创建一个测试用例,并且每次都需要从您的服务中提供一个合适的提要,因此我建议您使用spies,而不是引用真正的FlagService
- 创建一个全局fixture,这样您就不必为每个测试用例实例化它
- 您不需要调用。detectChanges,因为您已经调用了ngOnInit。
请参阅下面的项目,它涵盖了100%的appComponent。 ()。
- 您需要为每个if语句创建一个测试用例,并且每次都需要从您的服务中提供一个合适的提要,因此我建议您使用spies,而不是引用真正的FlagService
- 创建一个全局fixture,这样您就不必为每个测试用例实例化它
- 您不需要调用。detectChanges,因为您已经调用了ngOnInit。
请参阅下面的项目,它涵盖了100%的appComponent。 ()。
export interface Flag{'code':string;'desc':string;}export interface FlagModel{'flagDescription':Flag[];'employeeFlag':string;'region':Flag;'user':string;}
运行时,我正在获取类型脚本:无法读取未定义的属性“0”。我无法理解您的服务输出,您说您返回了一个FlagModel,但在subscribe中似乎没有。无论我以何种方式将新提交推送到git,它都会响应您在评论中描述的内容。我如何访问FlagModel,因为从FlagService observable返回的是FlagModel。您的可观察对象正在返回字符串。FlagModelexport interface Flag{'code':string;'desc':string;}export interface FlagModel{'flagDescription':Flag[];'employeeFlag':string;'region':Flag;'user':string;}
运行时,我正在获取类型脚本:无法读取未定义的属性“0”。我无法理解您的服务输出,您说您返回了一个FlagModel,但在subscribe中似乎没有。无论我以何种方式向git推送新的提交,它都会响应您在评论中描述的内容。