I've made an extremely simple service in order to check my sanity when it comes to testing. Here it is:
(function() {
'use strict';
angular
.module('app')
.service('TestService', TestService);
function TestService() {
function foo() {
bar();
}
function bar() {
console.log('Hello world!');
}
return {
foo: foo,
bar: bar
};
}
}());
So nothing complex here - foo() calls bar() which outputs a simple 'Hello World' message to the console. My test for this looks like this:
(function() {
'use strict';
fdescribe('test.service',function() {
var testService;
beforeEach(module('app'));
beforeEach(inject(function(_TestService_) {
testService = _TestService_;
}));
describe('setup',function() {
it('should get the testService',function() {
expect(testService).not.toBe(undefined);
});
});
describe('foo()',function() {
fit('should call bar',function() {
spyOn(testService,'bar');
testService.foo();
expect(testService.bar).toHaveBeenCalled();
});
});
});
}());
All I'm doing here is checking to see of foo calls bar in the usual method i.e. spying on bar. But this gives me the error:
Expected spy bar to have been called
I'm pulling my hair out trying to figure this one out as this cannot be something complicated - What am I fundamentally doing wrong?
Thanks!