Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm trying to figure out a way to test one my routes that has a really custom behavior. Here is the code of the state :

$stateProvider.state('oauth', {
  url: '/oauth/callback',
  controller: function($location, $window, ENV) {
    if ($location.hash()) {
      var pairs = $location.hash().split('&');
      var pair  = pairs[0].split('=');

      if (pair[1]) {
        localStorage.accessToken = pair[1];
        $window.location.href = '/';
      } else {
        $window.location.href = ENV.account;
      }
    }
  }
});

However, all I was able to do so far is :

describe('oauth', function() {
  beforeEach(function() {
    state = 'oauth';
  });

  it('should be the /oauth/callback url', function() {
    expect($state.href(state)).toEqual('/oauth/callback');
  });
});

I didn't find anything online that handle such a test case. Ideas?

share|improve this question

Use $state.get() to get the configuration of your state.

var config = $state.get('oauth');

then mock your controller's dependencies, instantiate your controller manually

var location = jasmine.createSpyObj('$location', ['hash']);
$injector.instantiate(config.controller, {}, {
    $location: location,
    // ... (provide mocks of the services required by your controller here)
});

and test any behavior you're expecting to happen

location.hash.and.returnValue('...');
expect(location.hash).toHaveBeenCalled();
expect($window.location.href).toBe('...');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.