Everything on the app works fine, so its just my tests that are syntactically incorrect. Here's a truncated version of what I'm working with. The names and locations have been changed to protect the innocent.
var m;
m = angular.module('CustomService', []);
window.CustomService = m.factory("CustomService", function($rootScope) {
var sharedService;
sharedService = {};
sharedService.broadcastItem = function() {
return console.log('Works!');
};
return sharedService;
});
window.MyCtrl = function($scope, $location, CustomService) {
this.$inject = ['$scope', 'CustomService'];
return $scope.test_method = function(date) {
return CustomService.broadcastItem(date);
};
};
describe('MyCtrl', function() {
beforeEach(inject(function($rootScope, $controller, $location) {
this.scope = $rootScope.$new;
return this.controller = $controller(MyCtrl, {
$scope: this.scope,
$location: $location,
CustomService: CustomService
});
}));
return it('tests my method', function() {
return this.scope.test_method('10-1984');
});
});
And that final line returns :
TypeError: Object #<Object> has no method 'test_method'
Strange! Because my entire app works and thrives off the fact that that method works flawlessly. So it must be that I'm not properly injecting this module ( guessing! ).
There were number of things going on in your code and test, really so it is a bit hard to list them all. It is kind of hard to completely help with this test as you didn't provide implementation to be tested (apart from the log) so I've stubbed something I think you would like to do and test.
So, here is the test:
describe('MyCtrl', function() {
var scope, controller;
beforeEach(module('CustomService'));
beforeEach(inject(function($rootScope, $controller, $location) {
scope = $rootScope.$new();
controller = $controller('MyCtrl', {
$scope: scope
});
}));
it('tests my method', function() {
scope.test_method('10-1984');
expect(scope.brodcastedValue).toEqual('10-1984');
});
});
The issues in the test were:
$new() is a method not a propertybeforeEach(module('CustomService'));I've also modified the code itself to make the test pass:
m.factory("CustomService", function($rootScope) {
var sharedService;
sharedService = {};
sharedService.broadcastItem = function(date) {
$rootScope.$broadcast('works', date);
};
return sharedService;
});
m.controller('MyCtrl', function($scope, $location, CustomService) {
$scope.test_method = function(date) {
CustomService.broadcastItem(date);
};
$scope.$on('works', function(event, date){
$scope.brodcastedValue = date;
});
});
Not sure if the above was your intention or not. Anyway, it looks like the code was converted from CoffeScript or something (full of return and this) so I'm not sure I get it right.
Finally, a working plunk, hopefully this one will clarify all the details: http://plnkr.co/edit/x2Jjvm8zwwaLZYV9aLfo?p=preview
I noticed one mistake in your syntax on this line:
this.scope = $rootScope.$new;
Instead of creating a new scope, you're referencing $rootScope's $new function. Try this:
this.scope = $rootScope.$new();