2017-06-21 142 views
0

我在写单元测试我的角度项目,打字稿打字稿+ jasmine.createSpy()

当我试图对某些服务创建模拟,我用这样的方式:

const serviceMock = <IMyService>{ 
    method: _.noop, 
}; 

beforeEach(inject($injector => { 
    testingService = new AccountingService(serviceMock); 

    spyOn(serviceMock, 'method').and.callFake(()=>'hello'); 
} 

该工程确定 但是当我试图用jasmine.createSpy(),我得到的编译错误:

const serviceMock = <IMyService>{ 
    method: jasmine.createSpy('method').and.callFake(()=>'hello'), 
}; 

Type '{ method: Spy;}' cannot be converted to type 'MyService'. Property 'getParams' is missing in type '{ method: Spy;}'. 

getParams是私有方法

我在做什么错?

回答

0

与映射类型

export type Spied<T> = { 
    [Method in keyof T]: jasmine.Spy; 
}; 

尝试它,与它投下您的服务模拟

const serviceMock = Spied<IMyService>{ 

看一看here的详细描述