angularjs - karma state go 无法解析 "..."

标签 angularjs testing karma-jasmine

我正在尝试在我的 Controller 中测试一个函数,只是测试是否调用了 $state.go。我在 $state.go 上放置了一个 spy ,但是当我调用该函数时,我得到“无法从状态中解析‘thestate’...”

这是我的 Controller 的功能:

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

和我的测试:

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

当它运行取消函数时,我收到“无法从状态中解析‘thestate’...”的错误,但我的 route 确实有一个已定义的状态。

如何模拟状态?

最佳答案

除非单元测试中测试的是UI Router,否则ui.router模块不需要加载,$state也需要mock与当前规范中未测试的任何其他服务一样。

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

...

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

关于angularjs - karma state go 无法解析 "...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37400157/

相关文章:

javascript - 自定义指令中的 ng-table 错误 "Cannot set property ' $data' of null"

testing - Arquillian - 如何以编程方式启动容器

javascript - Angular 6 + Universal Karma 测试导入模块

javascript - 使用 firebase 的即时模式自动登录

javascript - ng-if 语句以一种方式工作,但不以另一种方式工作,为什么?

Django Rest Framework APIClient 在测试期间不处理异常

javascript - 如果我不知道 Selenium 中的 ID 是什么,如何单击已生成 ID 的元素

angular - 测试 Angular HttpInterceptor

javascript - 使用 karma 和 jasmine 的 Angularjs 基本测试用例,不起作用

angularjs - Angular Bootstrap 折叠表行动画过渡不顺畅