Typescript 如何在单元测试中模拟TS类
考虑以下代码段:Typescript 如何在单元测试中模拟TS类,typescript,unit-testing,jasmine,Typescript,Unit Testing,Jasmine,考虑以下代码段: import {Calendar} from '@fullcalendar/core'; ... ngAfterViewInit() { this.calendar = new Calendar(this.element.nativeElement, config); this.calendar.render(); } 我正在使用fullcalendar插件,但它与我最初的问题无关,我认为,它可能是任何其他依赖项。因此该插件创建了一个日历视图。由fullcalen
import {Calendar} from '@fullcalendar/core';
...
ngAfterViewInit() {
this.calendar = new Calendar(this.element.nativeElement, config);
this.calendar.render();
}
我正在使用fullcalendar插件,但它与我最初的问题无关,我认为,它可能是任何其他依赖项。因此该插件创建了一个日历视图。由fullcalendar团队来测试日历行为,而我负责测试日历集成。因此,我需要测试日历是否已使用正确的配置进行初始化,因此我想省略真正的构造函数并检索参数。而且我不想创建日历的实例
如何在我的测试中模拟Calendar类?包装库调用是一种很好的做法。首先,测试它们更容易,如果库接口将更改,您只需在单个位置更改代码,并在代码的其余部分保留您自己的接口 因此,解决您的问题的一个解决方案是将日历创建包装在工厂服务中,如:
@Injectable({providedIn:'root'})
export class FullcalendarFactoryService{
public buildCalendar(element:HTMLElement,config:any){
return new Calendar(element,config);
}
}
在您的组件中,您必须注入factory服务,并像这样使用它:
constructor(public calenderFactory:FullcalendarFactoryService) {
}
ngAfterViewInit(): void {
this.calendar = this.calenderFactory.buildCalendar(this.element.nativeElement,this.config);
this.calendar.render();
}
为了进行测试,您可以简单地模拟工厂功能,如下所示:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
YourComponent
],
providers: [{
provide: FullcalendarFactoryService,
useClass: class {
buildCalendar = jasmine.createSpy('buildCalendar').and.returnValue({
render: () => true
});
}
}
]
}).compileComponents();
calendarFactory = TestBed.get(FullcalendarFactoryService);
}));
it('should call factory method with element and config', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
expect(calendarFactory.buildCalendar).toHaveBeenCalledWith(fixture.componentInstance.element.nativeElement, fixture.componentInstance.config);
});
import {FullcalendarFactoryService} from './fullcalendar-factory.service';
import {Calendar} from '@fullcalendar/core';
import dayGridPlugin from '@fullcalendar/daygrid'
describe('calendar factory service', () => {
let factory:FullcalendarFactoryService;
beforeEach(() => {
factory = new FullcalendarFactoryService();
})
it('should return calender instance',() => {
expect(factory.buildCalendar(document.createElement('test'),{plugins:[dayGridPlugin]})).toEqual(jasmine.any(Calendar))
})
})
更新:
要测试services buildCalendar函数是否返回日历实例,您需要测试您的服务,如下所示:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
YourComponent
],
providers: [{
provide: FullcalendarFactoryService,
useClass: class {
buildCalendar = jasmine.createSpy('buildCalendar').and.returnValue({
render: () => true
});
}
}
]
}).compileComponents();
calendarFactory = TestBed.get(FullcalendarFactoryService);
}));
it('should call factory method with element and config', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
expect(calendarFactory.buildCalendar).toHaveBeenCalledWith(fixture.componentInstance.element.nativeElement, fixture.componentInstance.config);
});
import {FullcalendarFactoryService} from './fullcalendar-factory.service';
import {Calendar} from '@fullcalendar/core';
import dayGridPlugin from '@fullcalendar/daygrid'
describe('calendar factory service', () => {
let factory:FullcalendarFactoryService;
beforeEach(() => {
factory = new FullcalendarFactoryService();
})
it('should return calender instance',() => {
expect(factory.buildCalendar(document.createElement('test'),{plugins:[dayGridPlugin]})).toEqual(jasmine.any(Calendar))
})
})
很好,但如何测试FullcalendarFactoryService的buildCalendar方法返回Calendar对象呢?这一部分还没有被发现。现在我可以测试参数,但它不会测试buildCalendar是否返回日历实例而不是未定义的实例。