我在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);
}]);
&#13;
我在Jasmine的测试:
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();
});
});
}
&#13;
我试过看过类似的问题,但没有任何帮助。任何建议都表示赞赏。
答案 0 :(得分:1)
this._mockService.GetPromotion(this._scope.prometricId).then(function (data: any) {
this._scope.promotion = data;
}, null);
您在此处的功能中丢失了this
个上下文。请改用箭头功能:
this._mockService.GetPromotion(this._scope.prometricId).then(data => {
this._scope.promotion = data;
}, null);