Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I spy on a mock service AngularJS / Karma?

'use strict'

webApp.controller 'NavigationController', [
  '$scope'
  '$rootScope'
  'UserService'
  ($scope, $rootScope, UserService) ->
    $scope.init = ->
      UserService.isAuthenticated().then (authenticated) ->
        $scope.isAuthenticated = authenticated

    $scope.init()
]

I want to write a test to spyOn if isAuthenticated was called from UserService. In my beforeEach, I have:

  beforeEach ->
    module 'webApp'

    inject ($injector) ->
      $httpBackend = $injector.get '$httpBackend'
      $q = $injector.get '$q'
      $rootScope = $injector.get '$rootScope'

      $scope = $rootScope.$new()
      $controller = $injector.get '$controller'

      UserServiceMock =
        isAuthenticated: ->
          deferred = $q.defer()
          deferred.promise


      controller = $controller 'AboutUsController',
        '$scope': $scope
        '$rootScope': $rootScope
        'UserService': UserServiceMock

      $httpBackend.whenGET('/api/v1/session').respond 200

Any help would be appreciated.. thanks

like image 673
Shamoon Avatar asked Aug 27 '14 13:08

Shamoon


People also ask

What is spy on in Angular?

Spy is a feature in Jasmine that allows you to spy on something to achieve the following goals: Monitor if a function is called along with the parameters pass to it. Override function return values or properties to simulate desired situations during tests. Override the implementation of functions completely.

What is karma in Angularjs?

Karma is a testing automation tool created by the Angular JS team at Google. The first step for using Karma is to install Karma. Karma is installed via npm (which is a package manager used for easy installation of modules on a local machine).

How do you mock variable in Jasmine?

First define a variable like this: let authService: AuthService; Then once the testbed has been set up, set this to the actual service being used by the TestBed inside the last beforeEach() : authService = TestBed.

What is angular mock module?

A mock module in Angular tests can be created by MockModule function. The mock module has the identical interface as its source module, but all its methods are dummies, and imports , declarations , providers and exports have been mocked respectively.


1 Answers

You can just set a variable to true when isAuthenticated is called in your UserServiceMock. e.g.:

var isAuthenticatedCalled;
var controller;

beforeEach(function() {
  isAuthenticatedCalled = false;

  module('webApp');
  inject(function($injector) {

    //...

    UserServiceMock = {
      isAuthenticated: function() {
        isAuthenticatedCalled = true;
        var deferred = $q.defer();
        deferred.resolve();
        return deferred.promise;
      }
    };
    controller = $controller('AboutUsController', {
      '$scope': $scope,
      '$rootScope': $rootScope,
      'UserService': UserServiceMock
    });

    // ...

  });
});

it('should call isAuthenticated', function() {
  expect(isAuthenticatedCalled).toBe(true)
});

Alternatively you could use Jasmine's spyOn function.

UserServiceMock = {
  isAuthenticated: function() {
    var deferred = $q.defer();
    deferred.resolve();
    return deferred.promise;
  }
};

spyOn(UserServiceMock, 'isAuthenticated');

And in your test you can do

it('should call isAuthenticated', function() {
  expect(UserServiceMock.isAuthenticated).toHaveBeenCalled()
});
like image 120
rob Avatar answered Nov 15 '22 11:11

rob