ngMockE2E $httpBackend "passThrough()" doesn't work
Hi, I'm trying to set up some test that actually do hit the backend, but I can't get this to work:
$httpBackend.whenPOST(/./).passThrough() $httpBackend.whenGET(/./).passThrough()
But I get the error: "POST /some/url No more request expected"
I am still able to use .whenGET().respond(), and it's variants, to mock the requests. Am I doing something wrong with passThrough()?
I too have this issue. As far as I can tell, the mocked $httpBackend keeps the "real" $httpBackend as a delegate but for some reason, this delegate is also a mocked $httpBackend, which does not expect the request resulting in this error.
Anyone got any feedback on this issue? I can't believe how hard it is to write an actual integration test in Angular... ugh.
I found that this worked for me when I was having that problem on my views folder. Maybe it's a start for you too.
$httpBackend.whenGET(/views\/.*/).passThrough();
(from here https://groups.google.com/forum/?fromgroups=#!topic/angular/ObdxCoCObYU)
And duplicate here: #2512
I don't see where #2512 is actually a duplicate. There, the problem is failing to actually instantiate ngMockE2E for the httpbackend. Here, the problem is that the ngMockE2E delegates passThrough() requests to ngMock httpbackend (i.e. doesn't use a real http backend as the delegate)
I am also having this issue.
As a workaround, I modified angular-mocks.js and commented out the registration of $HttpBackendProvider in ngMock module. This way, the $httpBackend in ngMockE2E will use the actual $httpBackend in ng module when passThrough option is used. In my specs where I was using ngMock.$httpBackend to mock the $http service, I added ngMockE2E as my module dependency to get my specs running.
I am having this issue too. This issue is reported from a year ago and still there...
I'm using version 1.2.9
If you are using ngAnimate and animating ngView, include the angular-animate.js after angular-mocks.js
this is my setup:
<script src="scripts/vendor/angular-1.2.9/angular.js"></script>
<script src="scripts/vendor/angular-1.2.9/angular-route.js"></script>
<script src="scripts/vendor/angular-1.2.9/angular-sanitize.js"></script>
<script src="scripts/vendor/angular-1.2.9/angular-resource.js"></script>
<script src="scripts/vendor/angular-1.2.9/angular-mocks.js"></script>
<script src="scripts/vendor/angular-1.2.9/angular-animate.js"></script>
<script src="scripts/app.js"></script>
and a sample app.js:
angular.module('app', ['ngRoute', 'ngAnimate', 'ngMockE2E'])
.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
'use strict';
$routeProvider
.when('/home', {
controller: 'HomeCtrl',
templateUrl: 'partials/home.html'
})
.otherwise( { redirectTo: '/home' } );
$locationProvider.html5Mode(false).hashPrefix('!');
}])
.run(['$httpBackend',
function ($httpBackend) {
$httpBackend.whenGET(/\.html$/).passThrough();
}
]);
I was having problems when including angular-animate.js before angular-mocks.js, I checked the network panel and the .html file was being downloaded correctly, then checked the elements panel and the DOM was being altered correctly too... changing the load order of the scripts worked for me.
I too have this issue. As far as I can tell, the mocked $httpBackend keeps the "real" $httpBackend as a delegate but for some reason, this delegate is also a mocked $httpBackend, which does not expect the request resulting in this error.
I also noticed this when trying to debug this issue.
In a "real" browser, the ngMockE2E backend keeps the real $httpBackend around as its $delegate. However, when I'm running Jasmine/Karma, for some reason, that delegate isn't defined, and the "real" $httpBackend's constructor never seems to be called at all.
I some more digging, and this appears to be an artifact of the fact that the angular-mocks injector (window.inject) automatically adds ng and ngMock as module dependencies to anything that it injects.
Loading ngMock and ngMockE2E at the same time appears to the injector's providerCache cache, which ultimately leads to ngMockE2E's $httpBackend $delegate pointing to ngMock's $httpBackend, rather than ng's $httpBackend (which is what the documented functionality is).
If we manually inject ng's $httpBackend into ngMockE2E, it looks like the delegate gets set correctly...
If I make the following change to angular-mocks.js, .passThrough() works correctly in Jasmine, and "normal" browsers:
@@ -1761,7 +1761,9 @@ angular.module('ngMock', ['ng']).provider({
* Currently there is only one mock present in this module -
* the {@link ngMockE2E.$httpBackend e2e $httpBackend} mock.
*/
-angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
+angular.module('ngMockE2E', ['ng']).service('$httpBackend', function(){
+ return angular.injector(['ng']).get('$httpBackend');
+}).config(['$provide', function($provide) {
$provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
}]);
@schmod why would you like to load both ngMock and ngMockE2E? Those are meant to be used for different tests types (unit vs. e2e). Mixing them sounds odd... What is your use-case?
I'm not trying to load both ngMock and ngMockE2E.
However, if I'm running Jasmine, and using the window.inject function, ngMock is provided for me, whether I want it or not:
window.inject = angular.mock.inject = function() {
...
modules.unshift('ngMock');
modules.unshift('ng');
There are a few rare cases when I actually might want to use passThrough() in a unit test, which would require me to include ngMockE2E.
@schmod ok, so what is your actual issue / reproduce scenario?
I have a service in my application that loads/caches images (in IndexedDB as a blob).
When writing unit tests around this service, I would like to be able to have Karma serve up a "real" image for consumption by my service. For practical reasons, it's easier to just include a real image in my Karma.conf and use a whenGET().passThrough() instead of .respond(). I'd imagine that you'd want to do something similar anytime you'd ever want to consume large amounts of data in your unit tests (to avoid polluting the test specs with the actual data).
To use .passThrough(), I need to add module('ngMockE2E'); in my test's beforeEach block.
When doing this, I inadvertently end up instantiating both ngMock and ngMockE2E, which gives me a broken passThrough() function on $httpBackend.
Reproduce senario (inside of Jasmine/Karma/Chrome):
describe('passThrough', function(){
var $http, $httpBackend, $rootScope;
beforeEach(function(){
module('ngMockE2E');
inject(function(_$http_, _$httpBackend_, _$rootScope_){
$http = _$http_;
$httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
});
});
it('should do something', function(){
$httpBackend.whenGET(/.*image.jpg$/).passThrough();
$http.get('image.jpg');
$rootScope.$apply();
});
});
This throws the error:
Error: Unexpected request: GET image.jpg
No more request expected
at $httpBackend (http://localhost:9050/base/app/scripts/angular-mocks.js:1207:9)
at $httpBackend (http://localhost:9050/base/app/scripts/angular-mocks.js:1200:11)
at sendReq (http://localhost:9050/base/app/bower_components/angular/angular.js:7819:9)
at $http.serverRequest (http://localhost:9050/base/app/bower_components/angular/angular.js:7553:16)
at wrappedCallback (http://localhost:9050/base/app/bower_components/angular/angular.js:10949:81)
at wrappedCallback (http://localhost:9050/base/app/bower_components/angular/angular.js:10949:81)
at http://localhost:9050/base/app/bower_components/angular/angular.js:11035:26
at Scope.$eval (http://localhost:9050/base/app/bower_components/angular/angular.js:11955:28)
at Scope.$digest (http://localhost:9050/base/app/bower_components/angular/angular.js:11781:31)
at Scope.$apply (http://localhost:9050/base/app/bower_components/angular/angular.js:12061:24)
Admittedly, this probably breaks the strict definition of what a "unit" test is, although there's also no good reason for this to be quite so broken...
@mstaessen unit test mocks and e2e mocks were never meant to be used together and I would suspect that there are many other issues with using both together.
Are you doing any actual assertions on the downloaded images?
$httpBackend is the only thing in the E2E mock, and we haven't had any other issues running with this configuration. I don't see any good reason why the two shouldn't be able to stack on top of each other.
(And, yes. We are doing assertions on the downloaded images. The service itself tries to determine the validity of the images that it downloads, which is why it's important that we should be able to send it real data during testing)
Similar issue being raised here: https://github.com/angular/angular.js/issues/3385
(Updated my comment above to mention the actual error that my reproduce scenario throws.)
I also have looked into this, because I have an expectGET and whenGET().passThrough to load a template file in a beforeEach.
On line 1165, it calls a $delegate function when definition.passThrough is true.
When this delegate (it calls the $httpBackend() again from line 1112) is called however, it seems to lose the expectation I gave it (probably due to expectations.shift() on line 1149 and it is therefore no longer set as wasExpected.
Is this the expected behaviour? I don't understand why it calls the mocked $httpBackend again when "passing through".