Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何在服务工厂中监视私有函数_Angularjs_Jasmine - Fatal编程技术网

Angularjs 如何在服务工厂中监视私有函数

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

对于下面的服务工厂,我想检查是否调用了foo()。还希望验证foo()函数的返回值。有谁能告诉我如何使用spyOn用Jasmine来做这个?我是Jasmine框架的新手

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