Skip to content

Unit Testing ui-router #537

@zdwolfe

Description

@zdwolfe

I'm having difficulties unit testing on state transition using Karma/Jasmine.
According to ui-router's own test/stateSpec.js, I might test state transition with something like the following:

it('should should transition to main.course', inject(function ($state, $q) {
    $state.transitionTo('main.course');
    $q.flush();
    expect($state.current).toBe('main.course');
  }));

However I'm getting an error:

TypeError: 'undefined' is not a function (evaluating '$q.flush()')

Can someone shed some light on this? I'd appreciate it. The error is very cryptic.

My full test is:

'use strict';

describe('Controller: CourseCtrl', function () {

  // load the controller's module
  beforeEach(module('myApp'));

  var CourseCtrl, scope;
  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();
    CourseCtrl = $controller('CourseCtrl', {
      $scope: scope
    });
  }));

  it('should should transition to main.course', inject(function ($state, $q) {
    $state.transitionTo('main.course');
    $q.flush();
  }));
});

I've recreated a very minimal application that tests this here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions