2

I have a $resource which I use to handle entity's crud http requests. It's wrapped in my custom service which at the moment doesn't add any functionality, so never mind it - it's just old plain $resource:

app.factory('coursesService', ['$resource', coursesService]);

function coursesService($resource) {
    return $resource('/api/courses/:id', {
  id: '@id'
}, {
  update: {
    method: 'PUT'
  }
});

}

So, I am trying to delete an instanse in my controller (which is stored in courseToDelete property of the controller object):

this.courseToDelete.$remove()

I am mocking backend with $httpBackend service from ngMockE2E module:

$httpBackend.whenDELETE(/api\/courses\/(.+)/, undefined, undefined, ['id'])
  .respond(function(method, url, data, headers, params) {
    clientStorage.deleteCourse(params.id);
    return [200, {}];
  });

So when $resource sends DELETE http request I want to be able to delete it from my localstorage (because I don't have a backend, data stored in localstorage as serialized JSON string)

The Object that I'm trying to delete:

 {
      id: 1,
      name: 'Course 1',
      duration: 380,
      description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' +
      ' Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.' +
      ' Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur' +
      ' sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
      created: new Date(2011,10,30),
      authors: [
        'Пушкин', 'Лермонтов'
      ]
    }

From the docs I expected that my params parameter will have id property on it but when the function executes params is undefined, although all other params look fine. What am I doing wrong here?enter image description here

dKab
  • 2,658
  • 5
  • 20
  • 35
  • How does the `courseToDelete` object look like? Does it have an Id property? – Gustav Dec 04 '15 at 14:25
  • @Gustav, I added this info to the question – dKab Dec 04 '15 at 14:28
  • I prefer $httpBackend.expectDELETE to $httpBackend.whenDELETE ... with expect you use $httpBackend.flush() in your unit test at the point when you expect the call to be made. This ensures that the CALL was made, else the test fails. whenDELETE does not cause the test to fail if the call was never made thus you cant be sure the call was ever made within the unit test. – danday74 Dec 04 '15 at 14:31

1 Answers1

1

From angular $httpBackend docs.

whenDELETE(url, [headers], [keys])

But you're using whenDELETE(url, undefined, undefined, [keys])

So your keys argument is undefined and nothing passed as params.

UPDATE: Note this feature wasnt implemented for 1.4.8 and earlier versions

Andrey
  • 4,020
  • 21
  • 35
  • You are saying that I should rewrite it like that: `$httpBackend.whenDELETE(/api\/courses\/(.+)/, undefined, ['id'])`? – dKab Dec 04 '15 at 14:32
  • Yes, exactly like that – Andrey Dec 04 '15 at 14:36
  • The result is the same – dKab Dec 04 '15 at 14:38
  • 1
    What version are you using? Docs written for 1.5.0 angular. This feature wasnt implemented for 1.4.8 and earlier versions – Andrey Dec 04 '15 at 14:57
  • I guess then I have no choice but to parse the url and extract the needed parameter manually. Ok, thanks! If edit your answer according to youe last comment, I will accept it. – dKab Dec 05 '15 at 06:37