【发布时间】:2019-03-07 13:40:03
【问题描述】:
我尝试使用 jasmine 和 angular 6 在我的电子应用程序中编写单元测试,但这种方式不起作用。我有一项服务,在我对另一项服务的特定测试中不需要测试。所以我决定像这样模拟第一个服务:
import { Injectable } from '@angular/core';
import { TestService } from '../../services/test/test.service';
@Injectable()
export class TestServiceMock extends TestService {
private somePath: string;
public isFileExistent(path: string): boolean {
return path === '\\some\\kind\\of\\path\\some.json' ? true : false;
}
}
我在 Visual Studio 代码中收到以下错误:
[ts] Class 'TestServiceMock' incorrectly extends base class 'TestService'.
Types have separate declarations of a private property 'somePath'.
我想我知道会发生这个错误,因为“somePath”成员与“TestService”中的成员有些不同,但它确实不是(在我的理解中)!这是“TestService”:
import {Injectable} from '@angular/core';
import {ElectronService} from '../electron/electron.service';
@Injectable()
export class TestService {
private somePath: string;
constructor(private electron: ElectronService) {
this.somePath = this.electron.remote.app.getAppPath();
}
public isFileExistent(path: string): boolean {
return // some boolean after a lot of operations not needed here
}
}
最后但并非最不重要的是我正在编写的测试显示以下错误:
[ts] Type 'typeof TestServiceMock' cannot be converted to type 'TestService'.
Property 'somePath' is missing in type 'typeof TestServiceMock'.
还有测试:
import { RealService } from './real.service';
import { TestServiceMock } from '../../response-models/test/test.service.mock';
import { TestService } from '../../services/test/test.service';
describe('RealService:', () => {
let realservice: RealService;
beforeEach(() => {
realservice = new RealService(TestServiceMock as TestService);
});
it('should be available:', () => {
expect(realservice).toBeDefined();
});
});
提前感谢您的帮助!
【问题讨论】:
标签: angular typescript unit-testing jasmine electron