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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With