我正在寻找一种方法来评估$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!
});
});
});发布于 2016-03-26 02:27:38
即使
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});可能足以抛出不想要的请求,将错误绑定到特定的规范使用:
expect($httpBackend.verifyNoOutstandingExpectation).not.toThrow();https://stackoverflow.com/questions/36222976
复制相似问题