Mossar
Mossar

Reputation: 435

Test angular controllers with Jasmine - module issue

I decided to learn how to test my angular code with Jasmine. Everything works when I don't use specific dependencies but if there are some dependencies I have problems. For example we have controllers.js:

angular.module('myApp', ['jmdobry.angular-cache'])

.controller('MyCtrl', ['$scope', '$http', function($scope, $http) {
    $scope.myName = "Wojtek";
    /...
}]);

And now I want to test:

describe('myApp', function() {
var scope,
controller;

beforeEach(angular.mock.module('myApp'));

describe('MyCtrl', function() {
    beforeEach(inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();
        controller = $controller('MyCtrl', {
            '$scope': scope
        });
    }));
    it('sets proper name', function () {
        expect(scope.myName).toBe("Wojtek");
    });
  });
});

My question is - how to mock that 'jmdobry.angular-cache' dependency?

Upvotes: 2

Views: 76

Answers (1)

Esteban Felix
Esteban Felix

Reputation: 1561

Since you don't need the actual mocking functionality for that module in your tests you can do something like this:

describe('myApp', function () {
  var scope,
    controller;

  beforeEach(angular.mock.module('jmdobry.angular-cache', [])); // just create a module that does nothing

  beforeEach(angular.mock.module('myApp'));

  describe('MyCtrl', function () {
    beforeEach(inject(function ($rootScope, $controller) {
      scope = $rootScope.$new();
      controller = $controller('MyCtrl', {
        '$scope': scope
      });
    }));
    it('sets proper name', function () {
      expect(scope.myName).toBe("Wojtek");
    });
  });
});

Upvotes: 1

Related Questions