[英]AngularJS $httpBackend verify no interactions
我正在尋找一種評估$httpBackend
的方法,以查看是否存在任何交互。 我想確保在我的測試案例中此刻從未調用過它。 我在這里檢查了文檔: https ://docs.angularjs.org/api/ngMock/service/ $ 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.