我有一个如下所示的 Controller
(function () {
var mockController = function ($scope, MockService) {
$scope.message = "This is a text message";
$scope.getCities = function () {
return MockService.getCities();
};
};
var mockService = function ($http) {
this.getCities = function () {
return $http.get("../rest/url", {
headers: {
'Accept': 'application/yang.data+json'
}
});
};
};
angular.module("MockApp", [])
.service("MockService", mockService)
.controller("MockController", mockController);
}())
我正在尝试编写一个 UT 来模拟如下服务
describe("MockController", function () {
var $scope;
beforeEach(function () {
module("MockApp");
inject(function (_$controller_, _$rootScope_, MockService) {
$scope = _$rootScope_.$new();
spyOn(MockService, "getCities").and.callFake(function () {
return [{
city: "Bangalore"
, country: "India"
}];
});
controller = _$controller_("MockController", {
$scope: $scope
});
});
});
describe("Test", function () {
it("Should be Bangalore", function () {
$scope.getCities()
.then(function (data) {
console.log("got it");
})
});
});
});
它抛出一个错误说
类型错误:$scope.getCities(...).then 不是一个函数
请帮助我。
最佳答案
我认为:
$scope.getCities = function () {
return MockService.getCities();
};
应该是:
$scope.getCities = function () {
return MockService(getCities());
};
关于javascript - 模拟服务时 Jasmine 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37211376/