Currently, I am in the process of testing a component that has Router injected in the constructor (TypeScript):
constructor(
private _router: Router,
private dispatcher: Observer<Action>,
fb: FormBuilder
) { ... }
Here are the test cases I have:
import {
it,
inject,
injectAsync,
beforeEachProviders,
TestComponentBuilder
} from "angular2/testing";
import {provide} from "angular2/core";
import {FormBuilder} from 'angular2/common';
import {Subject} from 'rxjs/Subject';
import {Router, RouteParams} from 'angular2/router';
// other imports ...
describe("SomeInfoEdit form", () => {
const actions = new Subject<Action>(null);
// providing implementations or mocks to the dependency injector
beforeEachProviders(() => {
return [
Router,
FormBuilder,
provide(dispatcher, { useValue: actions }),
SomeInfoEdit
];
});
it('should have default data', inject([SomeInfoEdit], (component) => {
expect(component.galtitle.value).toEqual("");
expect(component.friendlyUrl.value).toEqual("");
expect(component.isVisible.value).toBe(false);
}));
...
During the test execution, I encountered the following error:
Cannot resolve all parameters for 'Router'(?, ?, ?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Router' is decorated with Injectable.
I am interested in reading more about this to understand what is happening (I have gone through articles on DI).
It is worth mentioning that the actual code works without any issues, only the test is causing trouble.