业力状态go无法解决“......”

时间:2016-05-23 20:41:02

标签: angularjs testing karma-jasmine

我正在尝试在我的控制器中测试一个函数,然后测试是否调用了$state.go。我在$state.go上放了一个间谍,但是当我调用该函数时,我得到“无法从状态中解析'状态'......”

这是我的控制器的功能:

function cancel(){
  $state.go('thestate', {}, {reload: true});
}

和我的测试:

it('should change state to thestate', function() {
  this.scope.cancel();
});

当它运行取消功能时,我收到错误“无法从状态解析'状态'...”但我的路线中确实有一个已定义的状态。

我如何模仿州?

1 个答案:

答案 0 :(得分:3)

除非是在单元测试中测试的UI路由器,否则不需要加载ui.router模块并且必须模拟$state,以及任何其他不是在当前规范中测试。

beforeEach(module('app', { $state: {
  go: jasmine.createSpy()
}));

...

it('...', inject(function ($state) {
  this.scope.cancel();
  expect($state.go).toHaveBeenCalledWith('thestate', {}, {reload: true});
}));