feat(mock.$httpBackend): throw when nothing to flush, dump data/headers when expected different

This commit is contained in:
Vojta Jina 2011-10-31 11:36:31 -07:00 committed by Igor Minar
parent e3e2e4436e
commit a4c8ac7126
2 changed files with 47 additions and 26 deletions

42
src/angular-mocks.js vendored
View file

@ -591,12 +591,20 @@ angular.module.ngMock.$HttpBackendProvider = function() {
expectation = expectations[0],
wasExpected = false;
function prettyPrint(data) {
if (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
return data;
return angular.toJson(data);
}
if (expectation && expectation.match(method, url)) {
if (!expectation.matchData(data))
throw Error('Expected ' + method + ' ' + url + ' with different data');
throw Error('Expected ' + expectation + ' with different data\n' +
'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data);
if (!expectation.matchHeaders(headers))
throw Error('Expected ' + method + ' ' + url + ' with different headers');
throw Error('Expected ' + expectation + ' with different headers\n' +
'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' + prettyPrint(headers));
expectations.shift();
@ -648,15 +656,14 @@ angular.module.ngMock.$HttpBackendProvider = function() {
};
$httpBackend.flush = function(count) {
if (!responses.length) throw Error('No pending request to flush !');
count = count || responses.length;
while (count--) {
if (!responses.length) throw Error('No more pending requests');
if (!responses.length) throw Error('No more pending request to flush !');
responses.shift()();
}
};
$httpBackend.verifyExpectations = function() {
if (expectations.length) {
throw Error('Unsatisfied requests: ' + expectations.join(', '));
@ -674,6 +681,9 @@ angular.module.ngMock.$HttpBackendProvider = function() {
function MockHttpExpectation(method, url, data, headers) {
this.data = data;
this.headers = headers;
this.match = function(m, u, d, h) {
if (method != m) return false;
if (!this.matchUrl(u)) return false;
@ -684,29 +694,21 @@ function MockHttpExpectation(method, url, data, headers) {
this.matchUrl = function(u) {
if (!url) return true;
if (angular.isFunction(url.test)) {
if (!url.test(u)) return false;
} else if (url != u) return false;
return true;
if (angular.isFunction(url.test)) return url.test(u);
return url == u;
};
this.matchHeaders = function(h) {
if (angular.isUndefined(headers)) return true;
if (angular.isFunction(headers)) {
if (!headers(h)) return false;
} else if (!angular.equals(headers, h)) return false;
return true;
if (angular.isFunction(headers)) return headers(h);
return angular.equals(headers, h);
};
this.matchData = function(d) {
if (angular.isUndefined(data)) return true;
if (data && angular.isFunction(data.test)) {
if (!data.test(d)) return false;
} else if (data != d) return false;
return true;
if (data && angular.isFunction(data.test)) return data.test(d);
if (data && !angular.isString(data)) return angular.toJson(data) == d;
return data == d;
};
this.toString = function() {

View file

@ -531,7 +531,8 @@ describe('mocks', function() {
expect(function() {
hb('GET', '/match', null, noop, {});
}).toThrow('Expected GET /match with different headers');
}).toThrow('Expected GET /match with different headers\n' +
'EXPECTED: {"Content-Type":"application/json"}\nGOT: {}');
});
@ -541,7 +542,8 @@ describe('mocks', function() {
expect(function() {
hb('GET', '/match', 'different', noop, {});
}).toThrow('Expected GET /match with different data');
}).toThrow('Expected GET /match with different data\n' +
'EXPECTED: some-data\nGOT: different');
});
@ -589,11 +591,22 @@ describe('mocks', function() {
hb.when('GET').then(200, '');
hb('GET', '/url', null, callback);
expect(function() {hb.flush(2);}).toThrow('No more pending requests');
expect(function() {hb.flush(2);}).toThrow('No more pending request to flush !');
expect(callback).toHaveBeenCalledOnce();
});
it('(flush) should throw exception when no request to flush', function() {
expect(function() {hb.flush();}).toThrow('No pending request to flush !');
hb.when('GET').then(200, '');
hb('GET', '/some', null, callback);
hb.flush();
expect(function() {hb.flush();}).toThrow('No pending request to flush !');
});
it('respond() should set default status 200 if not defined', function() {
callback.andCallFake(function(status, response) {
expect(status).toBe(200);
@ -713,12 +726,18 @@ describe('mocks', function() {
it('should remove all responses', function() {
hb.expect('GET', '/url').respond(200, '', {});
hb('GET', '/url', null, callback, {});
var cancelledClb = jasmine.createSpy('cancelled');
hb.expect('GET', '/url').respond(200, '');
hb('GET', '/url', null, cancelledClb);
hb.resetExpectations();
hb.expect('GET', '/url').respond(300, '');
hb('GET', '/url', null, callback, {});
hb.flush();
expect(callback).not.toHaveBeenCalled();
expect(callback).toHaveBeenCalledOnce();
expect(cancelledClb).not.toHaveBeenCalled();
});
});