How to mock get(id) requests

若如初见. 提交于 2019-12-05 11:52:22

By using new function(url) your app tries to instantiate a new object from your anonymous function and pass that new object as the first argument of the $httpBackend.whenGET() call.
Of course, at the time of calling whenGET() no URL is provided, thus it is always undefined.

You should pass the function itself (and not an object instanciated using the function). E.g.:

$httpBackend.whenGET(function (url) {
  ...
}).respond(users[id]);

UPDATE:
After some more digging it turned out that the option to pass a function as the first argument to whenGET was added in version 1.3.0-beta.3. The docs you were reading probably referred to the latest beta version, while you were using an earlier version.
(Note that even versions 1.3.0-beta.1 and 2 did not provide this option.)

Without getting into much detail, responsible for verifying a matching URL is MockHttpExpectation's matchUrl method:

function MockHttpExpectation(method, url, data, headers) {
  ...
  this.matchUrl = function(u) {
    if (!url) return true;
    if (angular.isFunction(url.test)) return url.test(u);
    if (angular.isFunction(url)) return url(u);   // <<<<< this line does the trick
    return url == u;
  };

The line if (angular.isFunction(url)) return url(u); is the one that gives the option to directly pass a function and was added in version 1.3.0-beta.3 (as already mentioned). But, if you still want to pass a function to a previous AngularJS version, you could "trick" angular into believing you passed a RegExp, by providing an object with a test method.
I.e. replace:

.whenGET(function (url) {...})

with:

.whenGET({test: function (url) {...}})

See, also, this short demo.

I found a solution by using a function in the respond part instead of the when part:

$httpBackend.whenGET(new RegExp('\\/users\\/[0-9]+')).respond(
    function(method, url){
        var regexp = new RegExp('\\/users\\/([0-9]+)');
        var mockId = url.match(regexp)[1];
        return [200, users[mockId]];
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!