From 08daa7797bce5207916251d4a0ab3d5c93e5529a Mon Sep 17 00:00:00 2001 From: Ken Chen Date: Tue, 6 Aug 2013 16:53:46 +0100 Subject: feat(ngMock/$httpBackend): support a matching function for data param Add support for passing function as validating data: - To avoid hacking test method of RegExp - Optionally overwrite `toString` method of fn to show validation tips - change docs: param description for `when`, `whenPost`, `whenPut`, `expect`, `expectPost`, `expectPut`, `expectPATCH` Closes: #2981 --- test/ngMock/angular-mocksSpec.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test/ngMock/angular-mocksSpec.js') diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 893fe14c..e1873535 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1037,6 +1037,19 @@ describe('ngMock', function() { }); + it('should accept data as function', function() { + var dataValidator = function(data) { + var json = angular.fromJson(data); + return !!json.id && json.status === 'N'; + }; + var exp = new MockHttpExpectation('POST', '/url', dataValidator); + + expect(exp.matchData({})).toBe(false); + expect(exp.match('POST', '/url', '{"id": "xxx", "status": "N"}')).toBe(true); + expect(exp.match('POST', '/url', {"id": "xxx", "status": "N"})).toBe(true); + }); + + it('should ignore data only if undefined (not null or false)', function() { var exp = new MockHttpExpectation('POST', '/url', null); expect(exp.matchData(null)).toBe(true); -- cgit v1.2.3