Jasmine でテストを実行すると、そのエラーが発生します。モック ajax 呼び出しのテストを作成しようとしています。データ自体は重要ではありません。テストの実行を確認したいだけです。私のコード:
export interface IMockScope extends ng.IScope {
promotion: any;
prometricId: string;
}
export class MockController {
private _mockService: MockService;
private _scope: IMockScope;
constructor($scope: IMockScope, mockService: MockService) {
// assigning scope and service
this._mockService = mockService;
if ($scope === undefined) {
throw ("exception");
}
if (typeof($scope) === "MockController") {
console.log("I am the right type");
}
this._scope = $scope;
// getting a prometric ID for the promotion to load
this._scope.prometricId = "xxx";
// loading a promotion through a service call
this._mockService.GetPromotion(this._scope.prometricId).then(function (data: any) {
this._scope.promotion = data;
}, null);
}
}
export class MockService {
private _$http: ng.IHttpService;
private _$q: ng.IQService;
constructor($http: ng.IHttpService, $q: ng.IQService) {
this._$http = $http;
this._$q = $q;
}
GetPromotion(prometricId: string): ng.IPromise<any> {
var deferred = this._$q.defer();
var data = {
prometricId: prometricId
};
var promotion: any;
this._$http.get("/PreEval/GetPromotion").then(function (data: any) {
promotion = data;
deferred.resolve(promotion);
}, null);
return deferred.promise;
}
}
var mockApp = angular.module('mockApp', []);
//mockApp.controller('MyController', MyController);
mockApp.controller('MyController', ["$scope", "mockService", function ($scope: IMockScope, mockService: MockService) {
return new MockController($scope, mockService);
}]);
mockApp.factory("mockService", ["$http", "$q", function ($http: ng.IHttpService, $q: ng.IQService) {
return new MockService($http, $q);
}]);
ジャスミンでの私のテスト:
describe("Mock Controller", function () {
var controller: MockController;
var scope: IMockScope;
var $http: ng.IHttpService;
var $q: ng.IQService;
var mockService: MockService;
var backEnd: ng.IHttpBackendService;
beforeEach(function () {
angular.mock.module("mockApp");
});
beforeEach(angular.mock.inject(function ($rootScope: ng.IRootScopeService, _$http_: ng.IHttpService, _$q_: ng.IQService,
_mockService_: MockService, $httpBackend: ng.IHttpBackendService) {
scope = <IMockScope> $rootScope.$new();
$http = _$http_;
$q = _$q_;
mockService = _mockService_;
backEnd = $httpBackend;
backEnd.when("GET", "/PreEval/GetPromotion").respond(
{
"Success": true,
"ErrorMessage": "",
"Result": [
{ "ContactTypeId": 2, "Name": "Coworker" },
{ "ContactTypeId": 3, "Name": "Family" },
{ "ContactTypeId": 5, "Name": "Fresh" },
{ "ContactTypeId": 1, "Name": "Friend" }
]
});
}));
it("should have data", () => {
controller = new MockController(scope, mockService);
backEnd.flush();
expect(scope.promotion).toBeDefined();
});
});
}
同様の質問を見てみましたが、何も役に立ちませんでした。任意の提案をいただければ幸いです。