Parent Service:
module proj.Stuff {
export class ParentService {
//...properties, constructor, etc
public updateContent(id: number) {
this.dataService
.getContent(id)
.then((response) => this.content = response);
}
}
}
Child service:
module proj.Stuff {
export class ChildService{
//... properties, constructor, etc
public getContent(id: number) {
var request: IPromise<any> = this.$http.get(
ChildService.apiUrlBase + "getContent/" + id
);
return request
.then(response => {
return response.data.value;
}, response => {
this.$log.error("unable to get...");
});
}
}
}
Tests for the parent service:
describe("ParentService", () => {
// (property declarations omitted for brevity)
beforeEach(angular.mock.module(["$provide", ($provide) => {
var obj = {
getContent: (id: number) => {
functionCalled = true;
return {
then: (callback) => {
return callback(["result"]);
}
};
}
};
$provide.value("ChildService", obj);
}]));
beforeEach(mock.inject((_$http_, _$log_, _$q_, _$httpBackend_, _$rootScope_, _ChildService_) => {
cService = _ChildService_;
pParentService = new ParentService(cService);
}));
it("can be created", () => {
expect(pParentService).toBeDefined();
expect(pParentService).not.toBeNull();
});
it("can update content", () => {
pParentService.updateContent(1);
expect(pParentService.content).toEqual(["result"]);
expect(functionCalled).toBeTruthy();
// ***** what I want to do: *****
// expect(cService.getContent).toHaveBeenCalled();
});
});
I'm wondering how can I spy on cService.getContent instead of using the 'functionCalled' boolean?
When I try to spy on it, it complains that .then isn't defined - e.g. in the first beforeEach if I try spyOn(obj, "getContent") it doesn't like it.
The tests pass as is, but would rather spyOn instead of using the boolean.