Examples of unit testing the $routeProvider?

1,675 views
Skip to first unread message

John Walker

unread,
Jan 13, 2013, 12:33:37 AM1/13/13
to ang...@googlegroups.com
Hi
In the tutorial there are unit tests for controllers, directives, filters and services and the app wiring is tested in E2E testing with tests such as:

  it('should redirect index.html to index.html#/phones', function() {
    browser().navigateTo('../../app/index.html');
    expect(browser().location().url()).toBe('/phones');
  });

I was wondering if it is possible to test such routing from within a unit test. And if so how you would initilise the tests in the beforeEach method.

Thanks
John

Michael Dausmann

unread,
Jan 13, 2013, 7:37:16 AM1/13/13
to ang...@googlegroups.com
Hi John

Smells to me like testing 'such routing' fits squarely in 'e2e' or 'integration' testing, not unit testing so the approach you have outlined seems valuable.  There are a number of components at work during routing, $routeProvider, the browser and your code, to truly Unit test your code's part in this orchestration, you would need to 'mock out' both $routeProvider and the browser.

Maybe what you really want to test is that you are configuring the $routeProvider properly so maybe you can 'expect' the $routeProvider config?

Michael

John Walker

unread,
Jan 13, 2013, 5:16:50 PM1/13/13
to ang...@googlegroups.com
Hi Michael

> Maybe what you really want to test is that you are configuring the $routeProvider. properly

that's right - (my question was not particularly clear looking back on it :-)).

What I was wondering is if I could execute lower level set of test to logic such as:
  $routeProvider
      .when('/basket', {
        templateUrl: 'views/basket.html',
        controller: 'BasketCtrl'
      })
      .when('/checkout', {
        templateUrl: 'views/checkout.html',
        controller: 'CheckoutCtrl'
      })
      .otherwise({
        redirectTo: '/basket'
      });

when setPath to checkout then expect the routprovider to return  a templateurl of views/checkout.html' and controller: CheckoutCtrl


> so maybe you can 'expect' the $routeProvider config?

Can you expand on this?


Thanks
John

Joshua Miller

unread,
Jan 14, 2013, 2:38:16 AM1/14/13
to angular
Hello!

I'm not sure what value you get in E2E testing from just checking that you typed the correct value in the first time. But if you want to inspect the routes, you can activate the route and then look at the details using $route.current: http://docs.angularjs.org/api/ng.$route. Perhaps this is what you're looking for.

Josh


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

John Walker

unread,
Jan 15, 2013, 5:48:32 PM1/15/13
to ang...@googlegroups.com
thanks I'll try that

James Morgan

unread,
Mar 1, 2013, 4:48:16 AM3/1/13
to ang...@googlegroups.com
Did you ever work out how to unit test your routes?
Reply all
Reply to author
Forward
0 new messages