I have an angular service. Inside this service I have an object with a function, that references another function on the service. (Code below)
I want to use Jasmine (1.3) to spy on my service function, to verify that when the object's function gets called, it actually calls the real function.
My problem: After calling spyOn, the real function is still being called.
FooService.js
angular.module('foo').service("FooService", function() {
var self = this;
this.fooFunction = function() {
console.log("Foo function is being called");
}
this.bar = {
barFunction : self.fooFunction
}
});
FooService-spec.js
describe("Testing FooService", function() {
var service;
beforeEach(inject(function(_FooService_) {
service = _FooService_;
}));
describe("Test bar object", function() {
it("should call fooFunction when bar.barFunction is called", function() {
spyOn(service, "fooFunction");
service.bar.barFunction();
expect(service.fooFunction).toHaveBeenCalled();
});
});
});
I have found that if I change FooServce.js to the following, this all works though:
FooService - Working
angular.module('foo').service("FooService", function() {
var self = this;
this.fooFunction = function() {
console.log("Real function is being called");
}
this.bar = {
barFunction : function() {
return self.fooFunction();
}
}
});
What part of JavaScript / Angular / Jasmine am I failing to understand in the first example?