Vodonik
Vodonik

Reputation: 21

AngularJS testing a service with dependencies

Il go strait to it. Im writing unit tests for Angular front end application. This is what I have so far... app.js:

var app = angular.module('app', ['dependency1', 'dependency2', 'dependency3']);

Then to create a service I have:

app.factory('myService', ['serviceDependency', function(serviceDependency) {
    ...
});

How do I create/inject myService to be tested? When I do:

beforeEach(module("app"));

Jasmine screams at me about not finding dependencies, and I dont know how to mock them. (there are more than 3, like way more. :) )

beforeEach(inject(function(myService){}));

Does not work without doing the 'module' one first. Im stuck on this for 3 days googleing watching videos and I just cant find what I need, or I cant see it. First time writing for help, so do ask any questions if you feel I missed something, and you need more info.

ktnxbye

Edit:

Custom providers are created like so:

app.config(['customProvider', function(customProvider){
...
}]);

any suggestions on how to mock this?

ktnxbye

Upvotes: 1

Views: 307

Answers (1)

Vodonik
Vodonik

Reputation: 21

Well, I've 'solved' it! What I did is I included all dependencies required by the app in the test project and now it runs fine. But I don't quite like that solution. I would like it more if I could some how mock all those dependencies. I will still try to mock them and if I'm successful I will post here for all to see. Also I would appreciate any help from all :)

ktnxbye

Upvotes: 1

Related Questions