lundi 29 août 2016

Angular 2 Unit Testing: Injecting Typemoq objects

I'm implementing a unit test for a Service in Angular 2 (RC4) in Typescript. The service to test ("Calculation") uses a lot of other components ("Bausparen"), which on their part use components ("BausparenInput"). All components are declared @Injectable().

I tried to mock the depending components like Bausparen using Typemoq and injecting them before running the tests. Anyway, it doesn't work:

FAILED TESTS:
Calculation Service
× should return empty array when fromYear > toYear
  PhantomJS 2.1.1 (Windows 7 0.0.0)
factory
factory
resolveReflectiveFactory
resolveReflectiveProvider
map@[native code]
apply@[native code]
call@[native code]
call@[native code]
Call
map
resolveReflectiveProviders
resolve
resolveAndCreateChild
createInjector
execute

Inside Jasmines describe function, the code looks like this:

let calcInput = x => x.calc(TypeMoq.It.isAnyNumber(), TypeMoq.It.isAnyNumber());
let calcInputFormer = x => x.calc(TypeMoq.It.isAnyNumber(), TypeMoq.It.isAnyNumber(), TypeMoq.It.isAnyNumber());
let calcOutput = (y, m) => new CalcOutput();
let calcOutputFormer = (y, m, f) => new CalcOutput();
let bausparenMock = TypeMoq.Mock.ofType(Bausparen);
bausparenMock.setup(calcInput).returns(calcOutput);
bausparenMock.setup(calcInputFormer).returns(calcOutputFormer);

beforeEach(() => {
  addProviders([CalculationService,
    { provide: Bausparen, useClass: bausparenMock }]);
});

it('should return empty array when fromYear > toYear',
  inject([CalculationService], (calculation: CalculationService) => {
    let result = calculation.getValues(6, 2017, 6, 2016);
    expect(result.length).toBe(0);
}));

and signature of the class to mock is this:

class Bausparen implements Calculation {
  calc(year: number, month: number, formerAmount?: number): CalcOutput {...}
}

The error message doesn't really tell me anything. I don't even get if it's a problem in the setup of my mock or in injecting it. Has anybody had a similar error before?

Aucun commentaire:

Enregistrer un commentaire