Angularjs 在角度控制器内对注入服务进行单元测试
我的应用程序中有以下Angular控制器和服务设置(在Typescript中),我想为它们编写测试(使用Jasmine、Karma和PhantomJS) 请参阅下面的错误信息 有关完整的源代码,请参阅: 主控制器Angularjs 在角度控制器内对注入服务进行单元测试,angularjs,unit-testing,jasmine,phantomjs,karma-jasmine,Angularjs,Unit Testing,Jasmine,Phantomjs,Karma Jasmine,我的应用程序中有以下Angular控制器和服务设置(在Typescript中),我想为它们编写测试(使用Jasmine、Karma和PhantomJS) 请参阅下面的错误信息 有关完整的源代码,请参阅: 主控制器 namespace app.controllers { export class MainController implements app.Interfaces.IMainController { public city: string; public depar
namespace app.controllers {
export class MainController implements app.Interfaces.IMainController {
public city: string;
public departures: Interfaces.IQueryResult;
public arrivals: Interfaces.IQueryResult;
public error: string;
public time: number;
public nationalRail: Interfaces.INationalRailService;
static $inject: Array<string> = ['nationalRail', '$interval', '$routeParams'];
constructor(nationalRail: app.Interfaces.INationalRailService, private $interval: ng.IIntervalService, private $routeParams: Interfaces.IParameters) {
this.nationalRail = nationalRail;
this.city = $routeParams.City;
this.GetData();
this.$interval(() => {
this.GetData();
}, 60000);
this.$interval(() => {
this.time = Date.now();
}, 1000);
}
GetData(): void {
this.nationalRail.getDepartures(this.city).then((data: app.Interfaces.IQueryResult) => {
this.departures = data;
this.error = null;
}, () => { this.error = "Unable to load data for '" + this.city + "'"; });
this.nationalRail.getArrivals(this.city).then((data: app.Interfaces.IQueryResult) => {
this.arrivals = data;
this.error = null;
}, () => { this.error = "Unable to load data for '" + this.city + "'"; });
}
}
var appModule = angular.module('nationalRailViewer')
appModule.controller('MainController', ['nationalRail', '$interval', '$routeParams',
(nationalRail: app.Interfaces.INationalRailService, $interval: ng.IIntervalService, $routeParams: Interfaces.IParameters) => new MainController(nationalRail, $interval, $routeParams)]);
}
或类似的错误为下面的测试,如果我评论了前面的
为什么我的注入函数没有注入正确的对象
namespace app.services {
export class NationalRailService implements app.Interfaces.INationalRailService {
http: ng.IHttpService;
static $inject: Array<string> = ['$http'];
constructor($http: ng.IHttpService) {
this.http = $http;
}
getDepartures(city: string): ng.IPromise<app.Interfaces.IQueryResult> {
return this.http.get("http://the.service.location.net/departures/" + city)
.then(function(response) {
return response.data;
});
}
getArrivals(city: string): ng.IPromise<app.Interfaces.IQueryResult> {
return this.http.get("http://the.service.location.net/arrivals/" + city)
.then(function(response) {
return response.data;
});
}
}
angular.module('nationalRailViewer')
.service('nationalRail', ['$http', ($http: ng.IHttpService) => new NationalRailService($http)]);
}
describe("MainController Controller", function() {
var httpbackend: ng.IHttpBackendService;
var controller: app.Interfaces.IMainController;
var interval: ng.IIntervalService;
var routeParams: app.Interfaces.IParameters;
var nationalRail: app.Interfaces.INationalRailService;
var $controller: ng.IControllerService;
beforeEach(angular.mock.module("nationalRailViewer"));
beforeEach(() => angular.mock.inject(function($http: ng.IHttpService,
$httpBackend: ng.IHttpBackendService,
_nationalRail_: app.Interfaces.INationalRailService,
$controller: ng.IControllerService,
$interval: ng.IIntervalService,
$routeParams: app.Interfaces.IParameters) {
interval = $interval;
routeParams = $routeParams;
routeParams.City = "aberdeen";
httpbackend = $httpBackend;
nationalRail = _nationalRail_;
$controller = $controller;
}));
it("Should call getDepartures on NationalRailService", () => {
spyOn(nationalRail, "getDepartures").and.callFake(() => {});
controller = $controller<app.Interfaces.IMainController>('MainController', { nationalRail: nationalRail, $interval: interval, $routeParams: routeParams});
expect(httpbackend.expectGET("/departures/aberdeen")).toHaveBeenCalled();
httpbackend.flush();
});
it("Should call getArrivals on NationalRailService", () => {
spyOn(nationalRail, "getArrivals").and.callFake(() => {});
controller = $controller<app.Interfaces.IMainController>('MainController', { nationalRail: nationalRail, $interval: interval, $routeParams: routeParams});
expect(httpbackend.expectGET("arrivals/aberdeen")).toHaveBeenCalled();
httpbackend.flush();
});
});
Error: spyOn could not find an object to spy upon for getArrivals()