规格文件
'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
时,就会出现意外的get和$digest
错误。我该如何解决,请告诉我。
答案 0 :(得分:0)
在任何时间点只能进行一个$digest
或$ apply操作。这是为了防止很难检测到的错误进入您的应用程序。通过此错误的堆栈跟踪,可以跟踪引起错误的当前正在执行的$apply
或$digest
调用的起源。