-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot mock dependencies #167
Comments
Any idea why it does not work ? |
Ok so I found out that we need to add the module "ui.router" as dependency import {Component, Inject} from "ng-forward";
import {TeleportService} from "../../services/teleport/teleport.service";
@Component({
selector: "city",
templateUrl: "app/routes/city/city.html",
providers: ["ui.router", TeleportService]
})
@Inject("$stateParams", TeleportService)
export class CityRouteComponent {
details: any[];
constructor(private $stateParams: ng.ui.IStateParamsService, private teleportService: TeleportService) {}
ngOnInit() {
this.teleportService.fetchUrbanAreaDetails(this.$stateParams["slug"])
.then((result) => this.details = result);
}
} |
I found another problem now, using /// <reference path="../../../../../typings/index.d.ts" />
import {TestComponentBuilder, providers} from "ng-forward/cjs/testing";
import {Component} from "ng-forward";
import {CityRouteComponent} from "../city";
import {TeleportService} from "../../../services/teleport/teleport.service";
import IAugmentedJQuery = angular.IAugmentedJQuery;
declare var sinon: any;
describe("Component : CityRowComponent", function () {
let $q;
let $stateParams;
let tcb;
let fixture;
let testComponent;
let testComponentElement: IAugmentedJQuery;
let component: CityRouteComponent;
let componentElement: IAugmentedJQuery;
beforeEach(angular.mock.module("templates"));
beforeEach(inject((_$q_) => {
$q = _$q_;
}));
beforeEach(() => {
providers((provide) => {
let mockTeleportService = {
fetchUrbanAreaDetails: $q.when({key: "London"})
};
let mockStateParams = {
slug: "london"
};
return [
provide(TeleportService, {useValue: mockTeleportService}),
provide("$stateParams", {useValue: mockStateParams})
];
});
});
beforeEach(() => {
@Component({
selector: "test-component",
template: `<city></city>`,
directives: [CityRouteComponent]
})
class TestComponent {
}
tcb = new TestComponentBuilder();
fixture = tcb.create(TestComponent);
testComponent = fixture.componentInstance;
testComponentElement = fixture.nativeElement;
let childFixture = fixture.debugElement.componentViewChildren[0];
component = childFixture.componentInstance;
componentElement = childFixture.nativeElement;
});
it("renders the component", () => {
console.log(testComponent); // returns undefined
console.log(testComponentElement); // returns undefined
console.log(component); // returns undefined
console.log(componentElement); // returns undefined
expect(component.details).toEqual({key: "London"});
});
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I would like to test this component :
here is my spec file :
When I try to run this spec file, I have the following error and cannot run the tests.
When I remove the providers I do not have the errors but component is then null.
I am using Jasmine instead of Chai.
The text was updated successfully, but these errors were encountered: