Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using angularjs mocks without actually mocking anything

I'm writing some test for our app, and I've included angularjs-mocks.js so I can use the nice dsl functions that come with it.

But I don't want to actually mock the $http requests, since I'm integrating this app with a client's backend that is constantly in flux, and I want to test against real responses.

What's the best (easiest) way to use the real http backend in this situation?

EDIT:

I've found a potential solution, which is to use this:

$httpBackend.whenPOST(/.*/).passThrough()

But as I mention below, it's not working, possibly broken? I get this error:

Error: Unexpected request: POST /some/url/here No more request expected

I've opened an issue here: https://github.com/angular/angular.js/issues/1434

like image 546
doubledriscoll Avatar asked Oct 02 '12 02:10

doubledriscoll


1 Answers

I figured out a quick and dirty solution to the problem: Commenting out the line where $httpBackend is overridden (1365 in angular-mocks-1.0.2.js).

angular.module('ngMock', ['ng']).provider({
  $browser: angular.mock.$BrowserProvider,
  $exceptionHandler: angular.mock.$ExceptionHandlerProvider,
  $log: angular.mock.$LogProvider,
  //$httpBackend: angular.mock.$HttpBackendProvider,
  $rootElement: angular.mock.$RootElementProvider
}).config(function($provide) {
  $provide.decorator('$timeout', function($delegate, $browser) {
    $delegate.flush = function() {
      $browser.defer.flush();
    };
    return $delegate;
  });
});

This probably breaks something else, but it lets me test what I need to test, and doesn't affect the production app, so it's okay with me. Hopefully in the future there will be a built in way to do this.

like image 192
doubledriscoll Avatar answered Oct 16 '22 19:10

doubledriscoll