简体   繁体   English

AngularJS $ httpBackend验证没有交互

[英]AngularJS $httpBackend verify no interactions

I'm looking for a way to evaluate $httpBackend to see if there are any interactions. 我正在寻找一种评估$httpBackend的方法,以查看是否存在任何交互。 I want to make sure it has never been called at this point in my test case. 我想确保在我的测试案例中此刻从未调用过它。 I've checked the documentation here: https://docs.angularjs.org/api/ngMock/service/ $httpBackend and haven't found the answer. 我在这里检查了文档: https ://docs.angularjs.org/api/ngMock/service/ $ httpBackend,但没有找到答案。

Class using $http 使用$ 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;

test case so far. 到目前为止的测试用例。

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!
    });
  });
});

Even though 即使

  afterEach(function() {
   $httpBackend.verifyNoOutstandingExpectation();
   $httpBackend.verifyNoOutstandingRequest();
 });

may be enough to throw on unwanted requests, to tie an error to specific spec use: 可能足以引发不需要的请求,将错误与特定的规范用途相关联:

expect($httpBackend.verifyNoOutstandingExpectation).not.toThrow();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM