ps0604
ps0604

Reputation: 1071

What's missing in this Angular service Jasmine test?

In this plunk I have an Angular service that is tested by a Jasmine test. The test apparently cannot find the service, I get

Error: [$injector:unpr] http://errors.angularjs.org/1.6.1/$injector/unpr?p0=UtilsProvider%20%3C-%20Utils

and also

Error: Declaration Location

What's the problem here?

Javascript:

var app = angular.module("app", [])

app.service('Utils', function(){

    this.sum = function(a, b) {
          return a + b;
    };

});


describe("Testing Service Utils", function() {

    beforeEach(function() {
        angular.module("app");
    });

    it('should sum',
        inject(function(Utils) {
            expect(Utils.sum(1,2)).to.equal(3);
    }));

});

Upvotes: 1

Views: 212

Answers (1)

Phil
Phil

Reputation: 164744

Two problems...

  1. In order to register module configurations for testing, you need to use angular.mock.module

    beforeEach(angular.mock.module('app'))
    // or simply beforeEach(module('app'))
    
  2. The equality assertion is toEqual, not to.equal

    expect(Utils.sum(1,2)).toEqual(3)
    

http://plnkr.co/edit/UZfKILseNklw2MgO2l8c?p=preview

Upvotes: 1

Related Questions