1

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

  1. 在路由提供程序中我如何调用某些东西。

  2. 我如何测试返回结果是否符合我的预期。

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

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

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?
});

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

4

1 回答 1

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");
});
于 2016-03-04T07:00:49.917 回答