AngularJS$httpBackend验证无交互
我正在寻找一种方法来评估AngularJS$httpBackend验证无交互,angularjs,Angularjs,我正在寻找一种方法来评估$httpBackend,看看是否存在任何交互。我想确保在我的测试用例中从未调用过它。我已经检查了这里的文档:$httpBackend,还没有找到答案 使用$http初始化 class HomeService { /*@ngInject*/ constructor ($http, $log) { this.http = $http; this.log = $log; } submit(keycode) { this.log.log
$httpBackend
,看看是否存在任何交互。我想确保在我的测试用例中从未调用过它。我已经检查了这里的文档:$httpBackend,还没有找到答案
使用$http初始化
class HomeService {
/*@ngInject*/
constructor ($http, $log) {
this.http = $http;
this.log = $log;
}
submit(keycode) {
this.log.log("submitting key code: " + keycode);
if (keycode === "") {
return false;
}
this.http.post(`/api/keycode/${keycode}`).then ( (response) => {
this.log.log(response);
return true;
});
}
}
export default HomeService;
到目前为止的测试用例
import HomeService from './home.service';
describe('HomeService', () => {
let homeService, $httpBackend;
beforeEach(inject(($injector) => {
$httpBackend = $injector.get('$httpBackend');
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe('submit', () => {
it('submit empty keycode', () => {
homeService = new HomeService($httpBackend, console);
let value = homeService.submit("");
expect(value).to.be.false;
//valid no interactions with $httpBackend here!
});
});
});
即使
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
可能足以引发不必要的请求,从而将错误与特定规范使用联系起来:
expect($httpBackend.verifyNoOutstandingExpectation).not.toThrow();
HomeService应该注入$http,而不是$httpBackend。