使用$ httpBackend测试角度服务时,我得到了一些意想不到的结果。
当我使用httpBackend.expectGET运行测试时,测试按预期工作。但是,如果我使用whenGET运行完全相同的测试,测试将失败并显示消息'没有待处理的请求刷新'。
以下是受测试的服务:
'use strict';
describe('coverage-manager.js', function () {
describe('CoverageService', function () {
var httpBackend, sut, rootScope;
beforeEach(module('fot'));
describe('GetCoverageReport', function () {
beforeEach(inject(function ($httpBackend, CoverageService, $rootScope) {
httpBackend = $httpBackend;
sut = CoverageService;
rootScope = $rootScope;
}));
it('should return data', function () {
var expectedData = { testData: 'some data' };
var actualData;
httpBackend.expectGET('../../../js-test-reports/coverage/Chrome 43.0.2357%20(Windows%207)/cobertura-coverage.xml').respond(expectedData);
sut.GetCoverageReport()
.success(function (response) { actualData = response; });
httpBackend.flush();
expect(actualData).toEqual(expectedData);
});
it('should return data but says No pending request to flush', function () {
var expectedData = { testData: 'some data' };
var actualData;
httpBackend.whenGET('../../../js-test-reports/coverage/Chrome 43.0.2357%20(Windows%207)/cobertura-coverage.xml').respond(expectedData);
sut.GetCoverageReport()
.success(function (response) { actualData = response; });
rootScope.$digest();
httpBackend.flush();
expect(actualData).toEqual(expectedData);
});
it('should return data', function () {
var expectedData = { testData: 'some data' };
var actualData;
httpBackend.whenGET('../../../js-test-reports/coverage/Chrome 43.0.2357%20(Windows%207)/cobertura-coverage.xml').respond(expectedData);
sut.GetCoverageReport()
.success(function (response) { actualData = response; });
httpBackend.flush();
expect(actualData).toEqual(expectedData);
});
});
});
});
以下是测试:
Error: No pending request to flush !
at Function.$httpBackend.flush (file:///C:/dev/fot/git/client/src/main/js/bower_components/angular-mocks/angular-mocks.js:1455:34)
at Object.<anonymous> (file:///C:/dev/fot/git/client/src/test/javascript/spec/controllers/devTools/CoverageManager.spec.js:78:21)
at attemptSync (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1510:12)
at QueueRunner.run (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1498:9)
at QueueRunner.execute (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1485:10)
at Spec.queueRunnerFactory (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:518:35)
at Spec.execute (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:306:10)
at Object.<anonymous> (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1708:37)
at attemptAsync (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1520:12)
at QueueRunner.run (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1496:16)
&#13;
expectGET传递,当GET失败并且#34;没有待处理的冲洗请求#34;信息。我尝试使用$ rootScope。$ digest(),就像我在一些帖子中看到的那样,没有运气。除了expectGET和expectWHEN之外,测试是相同的,所以不确定发生了什么?
这是错误细节:
{{1}}