angularjs - 意外请求:GET 后跟 [$rootScope:inprog] $digest 已经在进行中。Angularjs 和 Karma
问题描述
规格文件
'use strict';
describe('component test : home', function () {
// add module reference you want to test
beforeEach(module('homeView'));
// add templates [from karma]
beforeEach(module('templates'));
var element;
var scope;
var $httpBackend;
beforeEach(inject(function ($rootScope, $compile, _$httpBackend_) {
$httpBackend = _$httpBackend_;
scope = $rootScope.$new();
element = angular.element('<home-view></home-view>');
scope.$apply(function () {
$compile(element)(scope);
});
}));
it('extra column http get', function () {
var mockResponse = {
data: {
success: true,
message: ':D :D'
}
};
$httpBackend.expectGET('http://localhost:51275/api/ExtraColumn')
.respond(mockResponse);
expect(data).toEqual(mockResponse);
$httpBackend.flush();
});
//it('header text', function () {
// var title = element.find('h1');
// console.log(title.text());
// expect(title.text()).toContain('Interviewees');
//});
});
错误
Error: Unexpected request: GET http://localhost:51275/api/ExtraColumn
No more request expected
at createFatalError (Scripts/angular-mocks.js:1569:19)
at $httpBackend (Scripts/angular-mocks.js:1616:11)
at sendReq (Scripts/angular.js:13257:9)
at serverRequest (Scripts/angular.js:12998:16)
at processQueue (Scripts/angular.js:17914:37)
at Scripts/angular.js:17962:27
at Scope.$digest (Scripts/angular.js:19075:15)
at ChildScope.$apply (Scripts/angular.js:19463:24)
at UserContext.<anonymous> (home-view/home-view.component.spec.js:19:15)
at Object.invoke (Scripts/angular.js:5122:19)
at <Jasmine>
at window.inject.angular.mock.inject (Scripts/angular-mocks.js:3422:25)
at Suite.<anonymous> (home-view/home-view.component.spec.js:15:16)
at <Jasmine>
at home-view/home-view.component.spec.js:3:1
ReferenceError: data is not defined
at <Jasmine>
at UserContext.<anonymous> (home-view/home-view.component.spec.js:36:16)
at <Jasmine>
我正在尝试为我的 angularjs 应用程序编写测试。测试需要模拟一个 http 请求,我在上面写了这个并得到了这些错误。$digest
当我的代码中什至没有 a 时,出现意外的 get 和已经在进行的错误$digest
。我该如何解决这个问题,请告诉我。
解决方案
在任何时间点,都只能有一个$digest
或 $apply 操作在进行中。这是为了防止很难检测到的错误进入您的应用程序。此错误的堆栈跟踪允许您跟踪导致错误的当前执行$apply
或$digest
调用的来源。
推荐阅读
- vb.net - 我在一个有值的变量上得到 System.NullReferenceException
- sql - 实体内同一实体的集合
- c++ - initializer_list 中的未初始化值(编译器错误?)
- gradle - 用于定义自定义存储库的 Gradle 函数 (Kotlin)
- php - + 符号在 PHP/SQLite 数据库中不起作用
- linux - PySide2 在构建 PyInstaller 时找不到 Qt 依赖项
- c++ - cl::Program::Sources 在哪里定义?
- ruby-on-rails - 如何在 Spree 中向 Checkout Flow 添加回滚转换事件
- node.js - 在保存或更新时自动设置猫鼬模式属性
- c++ - 铸造 shared_ptr
到weak_ptr 然后回来