I am learning how to write AngularJS tests using a stand-alone jasmine server only. I was successfully able to test a controller however, I'm having trouble testing a factory that I wrote. I get an error like,
Error: [$injector:modulerr]
(Factory code) myservice.js:
mod.factory('WeatherService', function ($http) {
return {
testingdata: function () {
return {name: "test", class: "ix"};
}
};});
app.js
var mod = angular.module("myapp", []);
my test:
describe("ws", function () {
describe('when I call WeatherService', function () {
it('returns 1', function () {
var t = {name: "test", class: "ix"};
var $injector = angular.injector(['myapp']);
var WeatherService= $injector.get('WeatherService');
expect(WeatherService.testingdata).toEqual(t);
});
});
});
Your test is unnecessarily complicated.
You do not use angular.injector in test - you can use module function from ngMock
Similarly instead of $injector.get you can use function inject from ngMock module.
describe("ws", function() {
beforeEach(module('myapp'));
describe('when I call WeatherService', function() {
it('returns 1', inject(function(WeatherService) {
var t = {
name: "test",
class: "ix"
};
expect(WeatherService.testingdata()).toEqual(t);
}));
});
});
here is working example http://plnkr.co/edit/doBJeWRyVyD75LfRP4G4?p=preview