需要帮助为 Angular 服务编写 Angular TypeScript Jasmine 测试

Need help writing an Angular TypeScript Jasmine test for an Angular Service

我正在尝试使用 TypeScript 为 Angular 服务编写测试。老实说,我不知道自己在做什么,因为这是我编写的第一个测试,所以我们将不胜感激。

我到目前为止给出了以下错误: TypeError: 'undefined' 不是一个对象(评估 'this.httpAccessor.get(requestUrl).then')

这是我要测试的服务:

/// <reference path="../../../typings/_references.ts" />

module app.service {
  'use strict';

  export interface IAvailability {
    getAvailability(requirements: app.Requirements): ng.IPromise<any>;
  }

  export class Availability implements IAvailability {

    static $inject = ['$filter', 'Service.HttpResourceAccessor'];

    /**
     * Constructor.
     */
    constructor(private $filter: ng.IFilterService,
      private httpAccessor: app.service.IHttpResourceAccessor) {
    }   

    /**
     *  Returns availability promise
     */
    getAvailability(requirements: app.Requirements): ng.IPromise<any> {

      /**
      * Generate the request url using the requirements
      */
      var requestUrl = this.generateRequestUrlFromRequirements(requirements);

      return this.httpAccessor.get(requestUrl).then((response) => {

        var availability: app.ITripAvailability<app.ITripAvailability>response;
        return availability;
      });
    }

    /**
    * Builds the request url using the requirements
    */
    private generateRequestUrlFromRequirements(requirements: app.Requirements): string {

      //The request string that will be returned after building
      var requestUrl: string = '';
      var baseAddress = 'http://localhost:8080/';

      //Code emitted as this just builds a string and works fine

      return requestUrl;
    }
  }

  angular.module('app.service')
    .service('Service.Availability', Availability);
}

这是目前的测试:

/// <reference path="../../../../typings/_references.ts" />
/// <reference path="../../../../typings/angularjs/angular-mocks.d.ts" />
/// <reference path="../../../../typings/jasmine/jasmine.d.ts" />

module app.service {

  describe("app.service.IAvailability", () => {
    'use strict';

    var $filter: ng.IFilterService;
    var mockAvailabilityService: app.service.Availability;
    var mockHttpAccessor: app.service.IHttpResourceAccessor;
    var requirements: app.Requirements;

    beforeEach(() => {
      angular.mock.module('app');
    });

    beforeEach(() => {
      angular.mock.module('app.service');
    });

    /**
    * Pre-test function before the test is executed
    */
    beforeEach(() => {

      requirements = new app.Requirements();

      angular.mock.inject((_$filter_: ng.IFilterService) => {
        $filter = _$filter_;

        mockHttpAccessor = <app.service.IHttpResourceAccessor>jasmine.createSpyObj(
          "httpAccessor", ["get"]);

        mockAvailabilityService = new app.service.Availability(
          <ng.IFilterService>$filter, 
          <app.service.IHttpResourceAccessor>mockHttpAccessor);
      });

    });

    it('Should call httpAccessor.get', () => {
      mockAvailabilityService.getAvailability(tripRequirements);
      expect(mockHttpAccessor.get).toHaveBeenCalled();
    });

    //How do I check if promise has been resolved?
  });
}

你的方向是正确的...

describe('app.service.IAvailability', ()=>{
  // declare dependencies and common vars
  var mockFilter: ng.IFilterService,
    mockHttpAccessor: app.service.IHttpResourceAccessor,
    requirements: app.Requirements,
    availabilityService: app.service.Availability;

  // setup dependencies/mocks
  beforeEach(()=>{
    angular.mock.module('app');
    angular.mock.inject((_$filter_: ng.IFilterService) => {
      mockFilter = _$filter_;
    });
    mockHttpAccessor = <app.service.IHttpResourceAccessor>jasmine.createSpyObj("httpAccessor", ["get"]); 
  });

  // helper function to create instance of the service
  // (useful if dependencies change, only 1 function needs changed)
  function createService(){
    availabilityService = new app.service.Availability(
      mockFilter,
      mockHttpAccessor);
  }

  // not a great test, but shows how to use the setup above
  it('should contain a getAvailability method', () => {
    createService();
    expect(availabilityService. getAvailability).toBeDefined();
  });
});