【发布时间】:2017-03-16 15:20:46
【问题描述】:
zippy.component.ts:
import { Component } from '@angular/core';
import {ZippyService} from '../services/zippy.service'
import {Renderer} from '@angular/core'
@Component({
selector: 'app-zippy',
templateUrl: './zippy.component.html',
styleUrls: ['./zippy.component.less']
})
export class ZippyComponent {
doNotShow:boolean = true;
text:string;
constructor(private zippyService:ZippyService, private renderer:Renderer) {
this.text = this.zippyService.getText();
}
toggleDisplay() {
this.doNotShow = !this.doNotShow;
}
}
zippy.component.spec.ts
describe('Zippy component shallow tests', ()=>{
let fixture:ComponentFixture<ZippyComponent>,
component: ZippyComponent;
let rendererMock = jasmine.createSpyObj('rendererMock', ['myFakeMethod']);
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ZippyComponent, ZippyPipe],
providers: [
{ provide: ZippyService, useValue: zippyServiceMock },
{ provide: Renderer, useValue: rendererMock }
],
schemas: [NO_ERRORS_SCHEMA]
});
TestBed.compileComponents().then(()=>{
fixture = TestBed.createComponent(ZippyComponent);
component = fixture.componentInstance;
});
}));
...
});
使用 karma 调试创建的组件表明正在注入 zippyServiceMock 而不是 ZippyService。 但不是 rendererMock,而是注入了真正的 Renderer。 如何在测试中注入 rendererMock 而不是真实的?
【问题讨论】:
标签: angular testing dependency-injection mocking