Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

TypeError: Cannot read property 'running' of undefined #1635

Closed
NickHeiner opened this Issue · 2 comments

2 participants

Nick Heiner Pawel Kozlowski
Nick Heiner

I'm trying to figure out how to mock out a service. This is what I'm trying:

'use strict';

var expect = chai.expect;

beforeEach(module('myModule'));

angular.module('mocks', []).factory('getPageName', function() {
    return function() {
        return "foo";
    }
});

describe('Foo', function() {
    it('should not crash', function() {
        module('mocks');
    });
});

Is it supposed to work? I get the following error:

Chrome 23.0 (Mac) Foo should not crash FAILED
    TypeError: Cannot read property 'running' of undefined
        at isSpecRunning (path/lib/angular/angular-mocks.js:1626:44)
        at window.module.angular.mock.module (path/lib/angular/angular-mocks.js:1648:12)
        at Context.<anonymous> (path/test/unit/directives/disclaimer.js:17:9)

I don't want to put the module() call in a beforeEach() because some tests need the mocks and others don't.

Pawel Kozlowski
Collaborator

@NickHeiner you've been almost there. If I understand you correctly you want to test a service that depends on another service and mock a dependency for each test. If so, let's say that we've got a car that has a dependency on an engine:

var app = angular.module('plunker', [])
  .factory('car', function(engine) {
     return {
       drive : function() {
         return 'Driving: ' + engine.speed();
       }
     }
  })
  .value('engine', {
    speed : function() {
      return 'fast';
    }
  });

Then you want to test a car an mock an engine. There are 2 ways of doing so: either by defining a new module in which we could redefine an engine:

describe('Testing a car', function() {
  var testEngine;

  beforeEach(function(){
    testEngine = {};
    angular.module('test', ['plunker']).value('engine', testEngine);
    module('test');
  });   

  it('should drive slow with a slow engine', inject(function(car) {
    testEngine.speed = function() {
      return 'slow';
    };
    expect(car.drive()).toEqual('Driving: slow');
  }));
});

A working plunk here: http://plnkr.co/edit/ueXIzk?p=preview

A bit simpler alternative, relaying on dynamic nature of JavaScript:

describe('Testing a car', function() {
  var testEngine;

  beforeEach(module('plunker'));
  beforeEach(inject(function(engine){
    testEngine = engine;
  }));

  it('should drive slow with a slow engine', inject(function(car) {
    testEngine.speed = function() {
      return 'slow';
    };
    expect(car.drive()).toEqual('Driving: slow');
  }));
});

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

Yet another alternative is to use a Jasmine's spy:

describe('Testing a car', function() {
  var testEngine;

  beforeEach(module('plunker'));
  beforeEach(inject(function(engine){
    testEngine = engine;
  }));

  it('should drive slow with a slow engine', inject(function(car) {
    spyOn(testEngine, 'speed').andReturn('slow');
    expect(car.drive()).toEqual('Driving: slow');
  }));
});

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

Closing this one as it seems to be more like a question than a bug report / feature request. You can always ask those types of questions on the very helpful mailing list (https://groups.google.com/group/angular, angular@googlegroups.com), IRC channel (#angularjs) or StackOverflow.

Nick Heiner

Ok, fair enough. I will use those forums unless I'm sure that there's actually a bug. I'm still having trouble making this work, so I'm continuing it on Stack Overflow:

http://stackoverflow.com/questions/13750892/mocking-in-unit-tests-with-mocha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.