如何在Angular 2中模拟泛型类

时间:2016-09-12 17:52:11

标签: javascript unit-testing angular typescript karma-runner

我想要模拟的课程是:

class Class<T>{ ... }

@Injectable()
Manager() { ... }

我有这个:

class FakeClass<T> {

constructor(public optionsForConstructor) {
.. do sth mock some methods etc.
}

 class FakeManager {

  public something;

  constructor() {
  public something: any = new FakeManager({
  ..some options not important here .. 
  });
}

我想用假类测试业力:

fdescribe('A test', () => {

let INTERVAL: number = 5000;

beforeEach(() => {
addProviders([
  SUT,
  {provide: MyClass, useClass: FakeClass<T>},
  {provide: Manager, useClass: FakeMapIncidentController},
]);

});

  fit('Should test something',
inject([MyClass, Manager], (myClass, manager) => {

   let SUT = new SUT(manager); // my SUT constructor needs Manager instance

   expect(true).toBe(true);
}));

但不幸的是,这不起作用。我明白了:

Cannot resolve all parameters for 'MyClass'(?). 
Make sure that all the    parameters are decorated with
Inject or have valid type annotations
and that 'MyClass' is decorated with Injectable. in...

它出了什么问题?它似乎应该工作。当我删除MyClass的提供行(因为它不是一个有角度的Injectable()服务)时,我得到提供程序错误。怎么解决这个?

0 个答案:

没有答案