0
我目前在我的Angular应用程序中的一个路径上设置了解析属性。我正在使用ngRoute。另外,我在我的解析函数中注入了一个名为Session
的服务。解决茉莉花测试中不可用的依赖关系
我的路线是这样的:
$routeProvider.when('/projects', {
templateUrl: 'views/projects/index.html',
controller: 'ProjectsCtrl',
resolve: {
beforeAction: function (Session) { // <-- here's the injection
// this logs an object in the browser,
// but in my test it logs as undefined
console.log('Session', Session);
}
}
});
在我的浏览器,这会将Session, Object {}
到我的控制台,符合市场预期。
但是,当我运行我的测试时,同一行将Session, undefined
打印到我的控制台。
我的测试是这样的:
beforeEach(module('visibilityApp'));
var route;
describe('/projects', function() {
beforeEach(inject(function ($route) {
route = $route;
}));
it('checks if the user is logged in', function() {
// Here I just invoke the function that's assigned to the
// route's resolve property, but Session then seems
// to be undefined.
route.routes['/projects'].resolve.beforeAction();
// then more of the test...
});
});
我已经发现,它并没有真正的问题是我注入的决心功能。如果我注入$location
并记录它,它是一样的:它在我的浏览器中工作,但在我作为测试运行时未定义。
我对Jasmine和Karma的测试。该应用程序由Yeoman生成。
为什么我的测试中未定义解析依赖关系?我的测试需要一些额外的设置吗?