$ state.go在ui-router中测试resolve时无法正常工作

时间:2015-01-02 18:13:11

标签: angular-ui-router karma-jasmine

$ state.go没有转换到default.settings.customization,而是转到我们的'main frontpage'default.feed

我认为这与解决方案有关,因为当我将var状态更改为其他状态时,它的工作正常。

app.config(['$stateProvider', function($stateProvider) {
  $stateProvider
.state('default.settings.customization', {
  url: '/settings/customization',
  templateUrl: 'app/settings/customization/customization.html',
  controller: 'CustomizationCtrl',
  auth: {
    authorizedRoles: ['admin']
  },
  resolve: {
    customization: [
      'CustomizationService',
      function(CustomizationService) {
        return CustomizationService.get().then(function(res) {
          return res.data;
        });
      }
    ]
  }
});
}]);

测试:

var state = 'default.settings.customization';

it('should resolve data', function() {
  customizationServiceMock.get = jasmine.createSpy('customizationServiceMockGet')
    .and.returnValue(apiResp.accountCustomization);
  $state.go(state);
  $rootScope.$apply();
  expect($state.current.name).toBe(state);
});

日志:

PhantomJS 1.9.8 (Linux) app/settings/customization router should resolve data FAILED
Expected 'default.feed' to be 'default.settings.customization'.
Error: Expected 'default.feed' to be 'default.settings.customization'.

1 个答案:

答案 0 :(得分:0)

我认为这与您的间谍功能有关,请检查您的示波器中是否有apiResp。我有一个类似的问题,我使用$ q在我的间谍功能中返回一个承诺,但没有注入$ q。