From 490d1f740ff14db8637a7c1e0f082937a9896c92 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Tue, 24 Jul 2018 19:45:29 +0300 Subject: [PATCH] fix(ngMock): pass failed HTTP expectations to $errorHandler This was only partially fixed in https://github.com/angular/angular.js/commit/f18dd2957caf1993bc2a3b4db7ebfeb797170aca --- src/ngMock/angular-mocks.js | 20 ++++++++++---- test/ngMock/angular-mocksSpec.js | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 0352bac958c3..363483940c3f 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1511,15 +1511,25 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) { } if (expectation && expectation.match(method, url)) { + var expectationError; + if (!expectation.matchData(data)) { - throw new Error('Expected ' + expectation + ' with different data\n' + - 'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data); + expectationError = new Error('Expected ' + expectation + ' with different data\n' + + 'EXPECTED: ' + prettyPrint(expectation.data) + '\n' + + 'GOT: ' + data); } if (!expectation.matchHeaders(headers)) { - throw new Error('Expected ' + expectation + ' with different headers\n' + - 'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' + - prettyPrint(headers)); + expectationError = new Error('Expected ' + expectation + ' with different headers\n' + + 'EXPECTED: ' + prettyPrint(expectation.headers) + '\n' + + 'GOT: ' + prettyPrint(headers)); + } + + if (expectationError) { + // In addition to be being converted to a rejection, this error also needs to be passed to + // the $exceptionHandler and be rethrown (so that the test fails). + expectationError.$$passToExceptionHandler = true; + throw expectationError; } expectations.shift(); diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 3470241fb2af..afecfbd4d739 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1506,6 +1506,33 @@ describe('ngMock', function() { }); + it('should throw error when expectation fails', function() { + expect(function() { + hb.expectPOST('/some', {foo: 1}).respond({}); + hb('POST', '/some', {foo: 2}, callback); + hb.flush(); + }).toThrowError(/^Expected POST \/some with different data/); + }); + + + it('should throw error when expectation about headers fails', function() { + expect(function() { + hb.expectPOST('/some', {foo: 1}, {X: 'val1'}).respond({}); + hb('POST', '/some', {foo: 1}, callback, {X: 'val2'}); + hb.flush(); + }).toThrowError(/^Expected POST \/some with different headers/); + }); + + + it('should throw error when expectation about both data and headers fails', function() { + expect(function() { + hb.expectPOST('/some', {foo: 1}, {X: 'val1'}).respond({}); + hb('POST', '/some', {foo: 2}, callback, {X: 'val2'}); + hb.flush(); + }).toThrowError(/^Expected POST \/some with different/); + }); + + it('should match headers if specified', function() { hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1'); hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2'); @@ -2833,6 +2860,24 @@ describe('ngMockE2E', function() { }).toThrowError('Unexpected request: GET /some\nNo more request expected'); }); + it('should throw error when expectation fails - without error callback', function() { + expect(function() { + hb.expectPOST('/some', { foo: 1 }).respond({}); + $http.post('/some', { foo: 2 }).then(noop); + + hb.flush(); + }).toThrowError(/^Expected POST \/some with different data/); + }); + + it('should throw error when unexpected request - with error callback', function() { + expect(function() { + hb.expectPOST('/some', { foo: 1 }).respond({}); + $http.post('/some', { foo: 2 }).then(noop, noop); + + hb.flush(); + }).toThrowError(/^Expected POST \/some with different data/); + }); + describe('passThrough()', function() { it('should delegate requests to the real backend when passThrough is invoked', function() {