Angular ng-mock - help matching url routes

90 views Asked by At

I'm trying to use ng-mock to fake a DELETE request but keep getting Error: Unexpected request: DELETE /api/1.0/package/1. How do I properly define the mock url to match the factory request:

function deletePackage(file) {
  return $http.delete('/api/1.0/package/' + file.id)
    .then(deleteComplete)
    .catch(deleteFailed)            
}

$httpBackend.whenDELETE('/api/1.0/package/:id').respond(function(method, url, data, headers, params) {
   return [200, params.id];
});
2

There are 2 answers

0
Phil On

So presumably you know what file argument you're going to pass to the deletePackage method. With that in mind, you should then know exactly what URI to expect.

var file = {id: 'some_id'};
$httpBackend.whenDELETE('/api/1.0/package/' + file.id).respond(file.id);

deletePackage(file); // presuming this function is in scope
0
neridaj On

I was looking at the wrong version of the docs (1.5) and I had to remove the single quotes around the regex:

$httpBackend.whenDELETE(/\/api\/1.0\/package\/(.+)/)
.respond(function(method, url, data) {
   return [200];
});