AngularJS - unit tests - using original service from mock of same service?

dprothero

I am trying to mock the $timeout service, which I know how to do, however, I need to be able to call the ORIGINAL $timeout service from within my mock. However, it just ends up with a stack overflow, recursively calling itself...

describe("myUnitTest", function () {
var $compile;
var $rootScope;
var $window;
var $timeout;

var timerTriggeredCount;

beforeEach(function () {
  module("myModuleBeingTested", function ($provide) {

    $provide.value("$timeout", fakeTimeout);

    function fakeTimeout(func) {
      timerTriggeredCount++;
      return $timeout(func, 1, false); // need this to call the original $timeout service
    }

    fakeTimeout.cancel = function(timer) {
      $timeout.cancel(timer); // need this to call the original $timeout servic
    }

  });

  inject(["$compile", "$rootScope", "$window", "$timeout", function (c, rs, w, t) {
    $compile = c;
    $rootScope = rs;
    $window = w;
    $timeout = t;
  }]);

});

....
dprothero

OK, I got it. I need to use a decorator (see here at the very end of the page):

describe("myUnitTest", function () {
var $compile;
var $rootScope;
var $window;
var $timeout;

var timerTriggeredCount;

beforeEach(function () {
  module("myModuleBeingTested", function ($provide) {

    var fakeTimeoutDecorator = [
      "$delegate", function ($delegate) {
        var oldTimeout = $delegate;
        var fakeTimeout = function (fn, delay, invokeApply) {
          return oldTimeout(function() {
            timerTriggeredCount++;
            fn();
          }, 1, invokeApply);
        };

        for (var prop in oldTimeout) {
          fakeTimeout[prop] = oldTimeout[prop];
        }

        return fakeTimeout;
      }
    ];

    $provide.decorator("$timeout", fakeTimeoutDecorator);

  });

....

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mock angularjs service for unit testing using jasmine

From Dev

AngularJS unit tests for database service

From Dev

Correct way to mock an AngularJS Service in a unit test?

From Dev

how to mock $window to unit test AngularJS service?

From Dev

AngularJS - Call function in service from the same service

From Dev

Mocking a service in unit tests

From Dev

How to Mock Service Initialization Parameters in Service Fabric for Unit Testing similar to mocking the state using a mock class?

From Dev

Unit Testing AngularJS Service

From Dev

Unit testing a service in angularJS

From Dev

Angularjs Unit Test for Service

From Dev

Angular unit tests, mocking a service that is injected using $injector

From Dev

how to mock angularJs filter value for unit tests

From Dev

Sinon to stub AngularJS service in Tests

From Dev

Mocking Service dependency in angularjs tests

From Dev

Unit Testing AngularJS and PouchDB Service

From Dev

Unit testing the AngularJS $window service

From Dev

Angularjs unit testing service with promise

From Dev

Unit Testing AngularJS and PouchDB Service

From Dev

What is wrong with this AngularJS/Jasmine unittest that tests a service using promises?

From Dev

AngularJS: Unit Testing Directive w/ Promise returned from a Service

From Dev

AngularJS: Unit Testing Directive w/ Promise returned from a Service

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

Grails Spock Mock Injected Service in Unit Test

From Dev

How to mock AngularFire 2 service in unit test?

From Java

How do I mock a service that returns promise in AngularJS Jasmine unit test?

From Dev

MassTransit Service Bus Configuration and Unit Tests

From Dev

Unit Tests Fails in Build Service But Not Locally

From Dev

Mocking an asynchronous web service in Angular unit tests

From Dev

EmberJS Service Injection for Unit Tests (Ember QUnit)

Related Related

  1. 1

    mock angularjs service for unit testing using jasmine

  2. 2

    AngularJS unit tests for database service

  3. 3

    Correct way to mock an AngularJS Service in a unit test?

  4. 4

    how to mock $window to unit test AngularJS service?

  5. 5

    AngularJS - Call function in service from the same service

  6. 6

    Mocking a service in unit tests

  7. 7

    How to Mock Service Initialization Parameters in Service Fabric for Unit Testing similar to mocking the state using a mock class?

  8. 8

    Unit Testing AngularJS Service

  9. 9

    Unit testing a service in angularJS

  10. 10

    Angularjs Unit Test for Service

  11. 11

    Angular unit tests, mocking a service that is injected using $injector

  12. 12

    how to mock angularJs filter value for unit tests

  13. 13

    Sinon to stub AngularJS service in Tests

  14. 14

    Mocking Service dependency in angularjs tests

  15. 15

    Unit Testing AngularJS and PouchDB Service

  16. 16

    Unit testing the AngularJS $window service

  17. 17

    Angularjs unit testing service with promise

  18. 18

    Unit Testing AngularJS and PouchDB Service

  19. 19

    What is wrong with this AngularJS/Jasmine unittest that tests a service using promises?

  20. 20

    AngularJS: Unit Testing Directive w/ Promise returned from a Service

  21. 21

    AngularJS: Unit Testing Directive w/ Promise returned from a Service

  22. 22

    How to mock AngularFire 2 service in unit test?

  23. 23

    Grails Spock Mock Injected Service in Unit Test

  24. 24

    How to mock AngularFire 2 service in unit test?

  25. 25

    How do I mock a service that returns promise in AngularJS Jasmine unit test?

  26. 26

    MassTransit Service Bus Configuration and Unit Tests

  27. 27

    Unit Tests Fails in Build Service But Not Locally

  28. 28

    Mocking an asynchronous web service in Angular unit tests

  29. 29

    EmberJS Service Injection for Unit Tests (Ember QUnit)

HotTag

Archive