jeudi 17 janvier 2019

Callback registered with respond is not executed when path is matched

I am trying to test an AngularJS directive which uses an HTML template through a reference. Because of that, it is issuing an http request.

What I am trying to do is capturing all requests for the templates using httpBackend.whenGET("....").respond(function) to load that template from the local store.

The point is that the function is never called. I know it is matching the path because it shows no error saying the call was not expected.

By the way, I prefer to do it this way instead of using html2js.

Let's give an example. If I set the backend.when wrong (non matching path) I get as error

Error: Unexpected request: GET resources/js/app/views/search/parts/con-search-filter/template.html No more request expected

If I execute in my code (and I know whenGET gets called):

httpBackend.whenGET(/(.*)template.html/, undefined, undefined, 
["basePath"]).respond(function(method, url, data, headers, params){
     debugger;
     console.log("hello");
     return [200, []];
});

Then, the previous error is gone. The point is the breakpoint (debugger;) is not hit and "hello" is not printed.

I am importing angular-mocks v1.5.11 and, according to the documentation, in the line 1255, I should be able to do it

$httpBackend.whenPATCH(/\/user\/(.+)\/article\/(.+)/, undefined, undefined, ['user', 'article'])
.respond(function(method, url, data, headers, params) {
// for url of '/user/1234/article/567' params is {user: '1234', article: '567'}
});

I am using jasmine-maven-plugin version 2.2, with the chrome driver. I am executing the bdd goal.

Aucun commentaire:

Enregistrer un commentaire