使用Karma测试AngularJS $ routeProvider

时间:2014-11-05 11:24:14

标签: angularjs jasmine karma-runner route-provider

我不确定如何测试routeProvider解析结果。

  1. 如何在路线提供者处打电话确定。

  2. 我如何测试返回结果是我期待的结果。

  3. 在以下示例中,这是我所缺少的:

    假设我有以下路由提供程序配置:

    var rt = angular.module("ResolveTest",[]);
    
    rt.config(["$routeProvider",function($routeProvider)
    {
      $routeProvider.when("/",{
        templateUrl: "rt.html",
        controller: "ResolveCtrl",
        resolve: {
          data: ["$q","$timeout",function($q,$timeout)
          {
            var deferred = $q.defer();
    
            $timeout(function()
            {
              deferred.resolve("my data value");
            },2000);
    
            return deferred.promise;
          }]
        }
      });
    }]);
    

    这是spec.js文件:

    describe("Testing route provide resolve", function() {
    
    var routes;
    
    
    it('should test routeProvider', function() {
    
          inject(function($route, $location, $rootScope) {
              routes = $route;
          });
    
          // here I would like to check that my data value was resolved from the route.
          // how can i do that?
    });
    

    有人可以提供一个完整的工作示例,说明如何调用正确的路线并测试解决方案吗?

1 个答案:

答案 0 :(得分:0)

我知道这已经老了,但我遇到了同样的问题,所以回答它。

已解析的数据会将其转换为 $ route.current.locals 属性。

您可以使用 -

进行测试
it('should test routeProvider', function() {

  inject(function($route, $location, $rootScope) {
      routes = $route;
  });

  // here I would like to check that my data value was resolved from the route.
  expect(routes.current.locals.data).toBe("my data value");
});