我正在尝试使用TypeScript为Angular Service编写测试。我会诚实地说我不知道我在做什么,因为这是我写过的第一个测试,所以任何帮助都会受到赞赏。
到目前为止,我发现了以下错误: 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?
});
}
答案 0 :(得分:1)
你正朝着正确的方向前进......
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();
});
});