我在AngularJS中有一个工作SPA,我想测试ui-router中的状态转换。
这是我的应用程序配置:
var myapp = angular.module('myapp', ['ui.bootstrap', 'ui.router', 'dialogs.main', 'ngSanitize']);
myapp.provider('AuthRule', function () {
return {
$get: function () {
return new AuthRules();
},
Rules: new AuthRules()
}
///////////////////////////////////////////////////////////////
function AuthRules()
{
/////variables defition
}
}
myapp.config(['$stateProvider', '$urlRouterProvider', '$httpProvider', 'AuthRuleProvider',
function ($stateProvider, $urlRouterProvider, $httpProvider, AuthRuleProvider) {
var AuthRule = AuthRuleProvider.Rules;
$urlRouterProvider.otherwise('/');
$stateProvider
.state("admin", {
url: "/admin",
templateUrl: "Views/admin/index.html",
controller: "AdminController",
auth: AuthRule.Admin
})
.state("admin.orders", {
url: "/orders",
templateUrl: "Views/admin/orders/index.html",
controller: "admin.OrdersController",
auth: AuthRule.Admin
})
.state("root.unauthorized", {
url: "unauthorized",
templateUrl: "Views/layout/unauthorized.html"
})
.state("root", {
url: "/",
templateUrl: "Views/content.html",
controller: "AppController",
auth: AuthRule.Root
});
}
]).run(function ($rootScope, $location, UserAuth, orderService, $state, AuthRule) {
UserAuth.authorize(AuthRule.Import.roles).then(function () {
$rootScope.user = UserAuth.getUser();
}, function () {
$rootScope.user = UserAuth.getUser();
});
var bypassRoute;
$rootScope.$on("$stateChangeStart", function (event, toState, toParams) {
if (bypassRoute) {
bypassRoute = false;
return;
}
var requireLogin = typeof toState.auth != 'undefined' &&
typeof toState.auth.requireLogin != 'undefined ' &&
toState.auth.requireLogin;
if (requireLogin) {
bypassRoute = true;
event.preventDefault();
var roles = typeof toState.auth != 'undefined' &&
typeof toState.auth.roles != 'undefined' &&
toState.auth.roles;
UserAuth.authorize(roles).then(
function () {
$state.go(toState, toParams);
},
function () {
$state.go("root.unauthorized");
});
}
});
});
茉莉花测试:
describe('State', function () {
var $rootScope, $state, $injector;
var $httpBackend, $rootScope, $q;
var userMock = { Name: 'Mr. User', Roles: ['Role1', 'Role2'] };
beforeEach(module('myApp'));
beforeEach(
inject(function (_$rootScope_, _$state_, _$injector_, _UserAuth_, _$httpBackend_) {
$rootScope = _$rootScope_;
$state = _$state_;
$injector = _$injector_;
$httpBackend = _$httpBackend_;
}));
beforeEach(
inject(function ($templateCache, $httpBackend) {
$templateCache.put('Views/admin/index.html', '');
$templateCache.put('Views/layout/unauthorized.html', '');
$httpBackend.when('GET', 'api/users').respond(userMock);
}));
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('root should be configured correctly', function () {
var state = 'root';
$state.go(state);
$httpBackend.flush();
expect($state.current.name).toBe(state);
});
it('root.undefined should be configured correctly', function () {
var state = 'root.unauthorized';
$state.go(state);
$httpBackend.flush();
expect($state.current.name).toBe(state);
});
it('admin should be configured correctly', function () {
var state = 'admin';
$state.go(state);
$httpBackend.flush();
$rootScope.$digest();
expect($state.current.name).toBe(state);
});
});
转换为'root.undefined'工作正常,但admin should be configured correctly
测试失败,并带有以下文字:
Result Message: Expected 'root' to be 'admin'.
所以$ state.go调用被重定向到'/',可能是因为缺少依赖或其他东西,但是我无法解决它,我无法调试测试,因为它是用Visual Studio编写的,并与Chutzpah一起运行。< / p>