Angularjs 如何在服务工厂中监视私有函数
对于下面的服务工厂,我想检查是否调用了foo()。还希望验证foo()函数的返回值。有谁能告诉我如何使用spyOn用Jasmine来做这个?我是Jasmine框架的新手Angularjs 如何在服务工厂中监视私有函数,angularjs,jasmine,Angularjs,Jasmine,对于下面的服务工厂,我想检查是否调用了foo()。还希望验证foo()函数的返回值。有谁能告诉我如何使用spyOn用Jasmine来做这个?我是Jasmine框架的新手 angular.module('myapp'). factory('messageservice', ['$http', '$q', function ($http, $q) { function foo(query) { return 'foo'; } function getServerMessages(quer
angular.module('myapp').
factory('messageservice', ['$http', '$q', function ($http, $q) {
function foo(query) {
return 'foo';
}
function getServerMessages(query) {
var query=foo();
return $http.get(query))
.then(function (result) {
console.log('success');
}, function (err) {
debugger;
console.log('error');
});
}
return {
getMessages: getFolderMessages,
getMessageDetails: getServerMessages
};
}]);
谢谢
奎师那你不能直接做到这一点,但你可以通过监视$http并查看作为参数传递的内容来间接做到这一点。如果得到正确的参数,您还知道调用了foo(),否则它将没有正确的值
spyOn($http, 'get').andReturn(aPromiseYouShouldResolveAtSomePointInYourTest);
expect($http.get).toHaveBeenCalled();
var queryItWasCalledWith = $http.get.mostRecentCall.args;
// do some assertions on queryItWasCalledWith