我有使用 $state 的服务。我正在尝试对服务进行单元测试。我看到 $state.current 在调用 $state.transitionTo 后没有改变。我检查了 $stateProvider,它包含状态。
路线配置:
angular.module('EmsWeb.Routes', ['ui.compat']).config(['$routeProvider', '$httpProvider', '$stateProvider', '$locationProvider',
function($routeProvider, $httpProvider, $stateProvider, $locationProvider) {
var defaultView = {
name: 'defaultView',
url: '',
template: '<div />'
};
var view = {
name: 'mainView',
url: '/view:nodeId:navParams:reqContext', //added reqContext in order to change state parameters that will trigger new navigation to view
templateUrl: 'MainView.html',
controller: 'MainAreaCtrl'
};
$stateProvider.state(defaultView);
$stateProvider.state(view);
$locationProvider.html5Mode(true);
}]);
服务代码:
angular.module('EmsWeb.Services').factory('MetadataNavigation', ['$q', '$state', '$stateParams', 'DalService', 'ResponseUtil', function ($q, $state, $stateParams, dalService, responseUtil) {
return {
hasChangedBroadcastData: function (nodeId, navParams, appContext) {
if (!$state.is('mainView'))
return true;
if ($stateParams.nodeId !=nodeId)
return true;
return false;
}
}]);
我的测试:
describe("Unit: MetadataNavigation->", function () {
var service,stateProvider;
beforeEach(angular.mock.module('EmsWeb.Routes', function ($stateProvider) {
stateProvider = $stateProvider;
}));
beforeEach(angular.mock.module('EmsWeb.Services'));
beforeEach(inject(function ($state, MetadataNavigation) {
service = MetadataNavigation;
$state.transitionTo('defaultView', {}, false);
//STATE WAS NOT CHANGED
}));
describe("hasChangedBroadcastdata:", function () {
var navParams1, navParams2, nodeId1, nodeId2, appContext1, appContext2, state;
beforeEach(inject(function ( AppModel) {
appContext1 = AppModel.context;
}));
it('broadcastdata has not changed',inject(function ($state) {
nodeId1 = 'nodeId1';
nodeId2 = 'nodeId2';
navParams1 = [{
targetName: 'fundId',
value: 'LPL.2q.xxxx',
display: 'ACLASS'
}, {
targetName: 'fundId',
value: 'LPL.2q.yyyy',
display: 'BCLASS'
}];
navParams2 = [{
targetName: 'fundId',
value: 'LPL.2q.xxxx',
display: 'ACLASS'
}, {
targetName: 'fundId',
value: 'LPL.2q.yyyy',
display: 'BCLASS'
}];
//quick way to copy w/o copying properties
appContext2 = angular.fromJson(angular.toJson(appContext1));
$state.transitionTo('mainView', { 'nodeId': nodeId1, 'navParams': navParams1, 'reqContext': appContext1.toRequestContext() }, false);
//STATE WAS NOT CHANGED
expect(service.hasChangedBroadcastData(nodeId2, navParams2, appContext2)).to.be.false;
}));
});
});
我查看了调试器,并且 $state.current 保持不变,即 $state.current.name 为空而 $state.current.abstract 为 true 我尝试了不同的方法无济于事。
我错过了什么?
谢谢你。