Angular.js: $httpbackend.expect with regular expression not working

Created on 25 Feb 2018  路  11Comments  路  Source: angular/angular.js

I'm submitting a ...

  • [x] bug report
  • [ ] feature request
  • [ ] other

Current behavior:
Am getting an Error: Unexpected request: POST /data/employee/1 No more request expected while trying to call to a mock backend service.

Expected / new behavior:

$httpBackend.expect does consume regular expression, as far as I can see my regex seems to be okay

Minimal reproduction of the problem with instructions:

聽In my聽app.config聽i've got my聽$stateProvider routes and my mocks as usual.

Mock with issue:

$httpBackend.expect("POST", /\/data\/employee\/(\d+)/, undefined, undefined,['id']).respond(function(method, url, data, headers, params){

        console.log('Received these data:', method, url, data, headers, params);

        return [200, {}, {}]
});

My controller:

app.controller("employeeController", ['$scope','$http', "$state", "$stateParams", function($scope, $http, $state, $stateParams){

   $http.post("/data/employee/1").then(function(response){
     //
   })
 })

AngularJS version: 1.x.y

angular v1.6.9
angular-mocks v1.6.9

Browser: [all | Chrome XX | Firefox XX | Edge XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

Using chrome 64

Anything else:

I've posted this issue on stackoverflow and have got no answers.

Please assist. Thanks.

All 11 comments

Can you provide a full running example? The snippets that you post look OK.
Also please try removing the router, since it doesn't appear to be relevant to your problem.

BTW, the error can also mean that the request is made twice. I.e. you only expect one request, but two requests are made (and the error is thrown at the second request. (If that is the case, it could be that the controller is instantiated twice - in which case the router _might_ be relevant.)

I am new to angular ve'been testing it out for 2 weeks now (hardcore jQuery fan :D), but really enjoying myself though.

@petebacondarwin
You can find the code here (My test project):
Payroll
app.Config
Controller

@gkalpak
This specific example executes the request twice, but even so, both times I get errors. I'm not sure why this happens.

What about the test code @samweru ?

@samweru
We usually don't check out test projects, we use https://plnkr.co to set up a runnable demo instead.

Btw, if you are starting with AngularJS development, you should consider using Angular (https://angular.io/) as this is the project that is actively developed.

@petebacondarwin I suppose $httpbackend.expect only works in the context of test setup? I haven't written any tests yet.

@Narretz don't I have the latest angularjs ? Downloaded it directly from the github repo.

@samweru you have the latest AngularJS version, but Angular (without JS) is the newer project. AngularJS will actually enter Long-term stability mode in mid-2018, i.e. only security and major bug fixes. Of course it'll still work, but Angular is your better bet for long term development.
If you want to continue with AngularJS, you should use a component based approach instead of ngController: https://docs.angularjs.org/guide/component

Regarding the httpBackend, if you are using it for backend-less development, you should use $httpBackend.when(), and not $httpBackend.expect(), see https://docs.angularjs.org/api/ngMockE2E/service/$httpBackend

@Narretz okay, thanks, will ensure with time that I migrate.

@Narretz @petebacondarwin I'm still learning Angular, so will now settle for $httpbackend.when which works fine before I can learn to do unit tests with jasmine

There is still one issue of requests being executed twice, how can I resolve this? Any tips?

Try debugging into the code to see when the calls to $httpBackend are happening. Then you will see what is triggering it.

Just to allay any fears, after understanding how $httpBackend.expect works, I've discovered it works just fine. See test http://embed.plnkr.co/7vJtqgmC7VwjyLZmmedD/ I apologize for the inconvenience.

Glad you got on top of the problem @samweru - thanks for the update.

Was this page helpful?
0 / 5 - 0 ratings