460 | *
461 | */
462 | angular.mock.TzDate = function (offset, timestamp) {
463 | var self = new Date(0);
464 | if (angular.isString(timestamp)) {
465 | var tsStr = timestamp;
466 |
467 | self.origDate = jsonStringToDate(timestamp);
468 |
469 | timestamp = self.origDate.getTime();
470 | if (isNaN(timestamp))
471 | throw {
472 | name: "Illegal Argument",
473 | message: "Arg '" + tsStr + "' passed into TzDate constructor is not a valid date string"
474 | };
475 | } else {
476 | self.origDate = new Date(timestamp);
477 | }
478 |
479 | var localOffset = new Date(timestamp).getTimezoneOffset();
480 | self.offsetDiff = localOffset*60*1000 - offset*1000*60*60;
481 | self.date = new Date(timestamp + self.offsetDiff);
482 |
483 | self.getTime = function() {
484 | return self.date.getTime() - self.offsetDiff;
485 | };
486 |
487 | self.toLocaleDateString = function() {
488 | return self.date.toLocaleDateString();
489 | };
490 |
491 | self.getFullYear = function() {
492 | return self.date.getFullYear();
493 | };
494 |
495 | self.getMonth = function() {
496 | return self.date.getMonth();
497 | };
498 |
499 | self.getDate = function() {
500 | return self.date.getDate();
501 | };
502 |
503 | self.getHours = function() {
504 | return self.date.getHours();
505 | };
506 |
507 | self.getMinutes = function() {
508 | return self.date.getMinutes();
509 | };
510 |
511 | self.getSeconds = function() {
512 | return self.date.getSeconds();
513 | };
514 |
515 | self.getTimezoneOffset = function() {
516 | return offset * 60;
517 | };
518 |
519 | self.getUTCFullYear = function() {
520 | return self.origDate.getUTCFullYear();
521 | };
522 |
523 | self.getUTCMonth = function() {
524 | return self.origDate.getUTCMonth();
525 | };
526 |
527 | self.getUTCDate = function() {
528 | return self.origDate.getUTCDate();
529 | };
530 |
531 | self.getUTCHours = function() {
532 | return self.origDate.getUTCHours();
533 | };
534 |
535 | self.getUTCMinutes = function() {
536 | return self.origDate.getUTCMinutes();
537 | };
538 |
539 | self.getUTCSeconds = function() {
540 | return self.origDate.getUTCSeconds();
541 | };
542 |
543 | self.getUTCMilliseconds = function() {
544 | return self.origDate.getUTCMilliseconds();
545 | };
546 |
547 | self.getDay = function() {
548 | return self.date.getDay();
549 | };
550 |
551 | // provide this method only on browsers that already have it
552 | if (self.toISOString) {
553 | self.toISOString = function() {
554 | return padNumber(self.origDate.getUTCFullYear(), 4) + '-' +
555 | padNumber(self.origDate.getUTCMonth() + 1, 2) + '-' +
556 | padNumber(self.origDate.getUTCDate(), 2) + 'T' +
557 | padNumber(self.origDate.getUTCHours(), 2) + ':' +
558 | padNumber(self.origDate.getUTCMinutes(), 2) + ':' +
559 | padNumber(self.origDate.getUTCSeconds(), 2) + '.' +
560 | padNumber(self.origDate.getUTCMilliseconds(), 3) + 'Z'
561 | }
562 | }
563 |
564 | //hide all methods not implemented in this mock that the Date prototype exposes
565 | var unimplementedMethods = ['getMilliseconds', 'getUTCDay',
566 | 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
567 | 'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
568 | 'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',
569 | 'setYear', 'toDateString', 'toGMTString', 'toJSON', 'toLocaleFormat', 'toLocaleString',
570 | 'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];
571 |
572 | angular.forEach(unimplementedMethods, function(methodName) {
573 | self[methodName] = function() {
574 | throw Error("Method '" + methodName + "' is not implemented in the TzDate mock");
575 | };
576 | });
577 |
578 | return self;
579 | };
580 |
581 | //make "tzDateInstance instanceof Date" return true
582 | angular.mock.TzDate.prototype = Date.prototype;
583 | })();
584 |
585 |
586 | /**
587 | * @ngdoc function
588 | * @name angular.mock.dump
589 | * @description
590 | *
591 | * *NOTE*: this is not an injectable instance, just a globally available function.
592 | *
593 | * Method for serializing common angular objects (scope, elements, etc..) into strings, useful for debugging.
594 | *
595 | * This method is also available on window, where it can be used to display objects on debug console.
596 | *
597 | * @param {*} object - any object to turn into string.
598 | * @return {string} a serialized string of the argument
599 | */
600 | angular.mock.dump = function(object) {
601 | return serialize(object);
602 |
603 | function serialize(object) {
604 | var out;
605 |
606 | if (angular.isElement(object)) {
607 | object = angular.element(object);
608 | out = angular.element('');
609 | angular.forEach(object, function(element) {
610 | out.append(angular.element(element).clone());
611 | });
612 | out = out.html();
613 | } else if (angular.isArray(object)) {
614 | out = [];
615 | angular.forEach(object, function(o) {
616 | out.push(serialize(o));
617 | });
618 | out = '[ ' + out.join(', ') + ' ]';
619 | } else if (angular.isObject(object)) {
620 | if (angular.isFunction(object.$eval) && angular.isFunction(object.$apply)) {
621 | out = serializeScope(object);
622 | } else if (object instanceof Error) {
623 | out = object.stack || ('' + object.name + ': ' + object.message);
624 | } else {
625 | out = angular.toJson(object, true);
626 | }
627 | } else {
628 | out = String(object);
629 | }
630 |
631 | return out;
632 | }
633 |
634 | function serializeScope(scope, offset) {
635 | offset = offset || ' ';
636 | var log = [offset + 'Scope(' + scope.$id + '): {'];
637 | for ( var key in scope ) {
638 | if (scope.hasOwnProperty(key) && !key.match(/^(\$|this)/)) {
639 | log.push(' ' + key + ': ' + angular.toJson(scope[key]));
640 | }
641 | }
642 | var child = scope.$$childHead;
643 | while(child) {
644 | log.push(serializeScope(child, offset + ' '));
645 | child = child.$$nextSibling;
646 | }
647 | log.push('}');
648 | return log.join('\n' + offset);
649 | }
650 | };
651 |
652 | /**
653 | * @ngdoc object
654 | * @name ngMock.$httpBackend
655 | * @description
656 | * Fake HTTP backend implementation suitable for unit testing application that use the
657 | * {@link ng.$http $http service}.
658 | *
659 | * *Note*: For fake http backend implementation suitable for end-to-end testing or backend-less
660 | * development please see {@link ngMockE2E.$httpBackend e2e $httpBackend mock}.
661 | *
662 | * During unit testing, we want our unit tests to run quickly and have no external dependencies so
663 | * we don’t want to send {@link https://developer.mozilla.org/en/xmlhttprequest XHR} or
664 | * {@link http://en.wikipedia.org/wiki/JSONP JSONP} requests to a real server. All we really need is
665 | * to verify whether a certain request has been sent or not, or alternatively just let the
666 | * application make requests, respond with pre-trained responses and assert that the end result is
667 | * what we expect it to be.
668 | *
669 | * This mock implementation can be used to respond with static or dynamic responses via the
670 | * `expect` and `when` apis and their shortcuts (`expectGET`, `whenPOST`, etc).
671 | *
672 | * When an Angular application needs some data from a server, it calls the $http service, which
673 | * sends the request to a real server using $httpBackend service. With dependency injection, it is
674 | * easy to inject $httpBackend mock (which has the same API as $httpBackend) and use it to verify
675 | * the requests and respond with some testing data without sending a request to real server.
676 | *
677 | * There are two ways to specify what test data should be returned as http responses by the mock
678 | * backend when the code under test makes http requests:
679 | *
680 | * - `$httpBackend.expect` - specifies a request expectation
681 | * - `$httpBackend.when` - specifies a backend definition
682 | *
683 | *
684 | * # Request Expectations vs Backend Definitions
685 | *
686 | * Request expectations provide a way to make assertions about requests made by the application and
687 | * to define responses for those requests. The test will fail if the expected requests are not made
688 | * or they are made in the wrong order.
689 | *
690 | * Backend definitions allow you to define a fake backend for your application which doesn't assert
691 | * if a particular request was made or not, it just returns a trained response if a request is made.
692 | * The test will pass whether or not the request gets made during testing.
693 | *
694 | *
695 | *
696 | *
Request expectations
Backend definitions
697 | *
698 | *
Syntax
699 | *
.expect(...).respond(...)
700 | *
.when(...).respond(...)
701 | *
702 | *
703 | *
Typical usage
704 | *
strict unit tests
705 | *
loose (black-box) unit testing
706 | *
707 | *
708 | *
Fulfills multiple requests
709 | *
NO
710 | *
YES
711 | *
712 | *
713 | *
Order of requests matters
714 | *
YES
715 | *
NO
716 | *
717 | *
718 | *
Request required
719 | *
YES
720 | *
NO
721 | *
722 | *
723 | *
Response required
724 | *
optional (see below)
725 | *
YES
726 | *
727 | *
728 | *
729 | * In cases where both backend definitions and request expectations are specified during unit
730 | * testing, the request expectations are evaluated first.
731 | *
732 | * If a request expectation has no response specified, the algorithm will search your backend
733 | * definitions for an appropriate response.
734 | *
735 | * If a request didn't match any expectation or if the expectation doesn't have the response
736 | * defined, the backend definitions are evaluated in sequential order to see if any of them match
737 | * the request. The response from the first matched definition is returned.
738 | *
739 | *
740 | * # Flushing HTTP requests
741 | *
742 | * The $httpBackend used in production, always responds to requests with responses asynchronously.
743 | * If we preserved this behavior in unit testing, we'd have to create async unit tests, which are
744 | * hard to write, follow and maintain. At the same time the testing mock, can't respond
745 | * synchronously because that would change the execution of the code under test. For this reason the
746 | * mock $httpBackend has a `flush()` method, which allows the test to explicitly flush pending
747 | * requests and thus preserving the async api of the backend, while allowing the test to execute
748 | * synchronously.
749 | *
750 | *
751 | * # Unit testing with mock $httpBackend
752 | *
753 | *
754 | // controller
755 | function MyController($scope, $http) {
756 | $http.get('/auth.py').success(function(data) {
757 | $scope.user = data;
758 | });
759 |
760 | this.saveMessage = function(message) {
761 | $scope.status = 'Saving...';
762 | $http.post('/add-msg.py', message).success(function(response) {
763 | $scope.status = '';
764 | }).error(function() {
765 | $scope.status = 'ERROR!';
766 | });
767 | };
768 | }
769 |
770 | // testing controller
771 | var $httpBackend;
772 |
773 | beforeEach(inject(function($injector) {
774 | $httpBackend = $injector.get('$httpBackend');
775 |
776 | // backend definition common for all tests
777 | $httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'});
778 | }));
779 |
780 |
781 | afterEach(function() {
782 | $httpBackend.verifyNoOutstandingExpectation();
783 | $httpBackend.verifyNoOutstandingRequest();
784 | });
785 |
786 |
787 | it('should fetch authentication token', function() {
788 | $httpBackend.expectGET('/auth.py');
789 | var controller = scope.$new(MyController);
790 | $httpBackend.flush();
791 | });
792 |
793 |
794 | it('should send msg to server', function() {
795 | // now you don’t care about the authentication, but
796 | // the controller will still send the request and
797 | // $httpBackend will respond without you having to
798 | // specify the expectation and response for this request
799 | $httpBackend.expectPOST('/add-msg.py', 'message content').respond(201, '');
800 |
801 | var controller = scope.$new(MyController);
802 | $httpBackend.flush();
803 | controller.saveMessage('message content');
804 | expect(controller.status).toBe('Saving...');
805 | $httpBackend.flush();
806 | expect(controller.status).toBe('');
807 | });
808 |
809 |
810 | it('should send auth header', function() {
811 | $httpBackend.expectPOST('/add-msg.py', undefined, function(headers) {
812 | // check if the header was send, if it wasn't the expectation won't
813 | // match the request and the test will fail
814 | return headers['Authorization'] == 'xxx';
815 | }).respond(201, '');
816 |
817 | var controller = scope.$new(MyController);
818 | controller.saveMessage('whatever');
819 | $httpBackend.flush();
820 | });
821 |
822 | */
823 | angular.mock.$HttpBackendProvider = function() {
824 | this.$get = [createHttpBackendMock];
825 | };
826 |
827 | /**
828 | * General factory function for $httpBackend mock.
829 | * Returns instance for unit testing (when no arguments specified):
830 | * - passing through is disabled
831 | * - auto flushing is disabled
832 | *
833 | * Returns instance for e2e testing (when `$delegate` and `$browser` specified):
834 | * - passing through (delegating request to real backend) is enabled
835 | * - auto flushing is enabled
836 | *
837 | * @param {Object=} $delegate Real $httpBackend instance (allow passing through if specified)
838 | * @param {Object=} $browser Auto-flushing enabled if specified
839 | * @return {Object} Instance of $httpBackend mock
840 | */
841 | function createHttpBackendMock($delegate, $browser) {
842 | var definitions = [],
843 | expectations = [],
844 | responses = [],
845 | responsesPush = angular.bind(responses, responses.push);
846 |
847 | function createResponse(status, data, headers) {
848 | if (angular.isFunction(status)) return status;
849 |
850 | return function() {
851 | return angular.isNumber(status)
852 | ? [status, data, headers]
853 | : [200, status, data];
854 | };
855 | }
856 |
857 | // TODO(vojta): change params to: method, url, data, headers, callback
858 | function $httpBackend(method, url, data, callback, headers) {
859 | var xhr = new MockXhr(),
860 | expectation = expectations[0],
861 | wasExpected = false;
862 |
863 | function prettyPrint(data) {
864 | return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
865 | ? data
866 | : angular.toJson(data);
867 | }
868 |
869 | if (expectation && expectation.match(method, url)) {
870 | if (!expectation.matchData(data))
871 | throw Error('Expected ' + expectation + ' with different data\n' +
872 | 'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data);
873 |
874 | if (!expectation.matchHeaders(headers))
875 | throw Error('Expected ' + expectation + ' with different headers\n' +
876 | 'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' +
877 | prettyPrint(headers));
878 |
879 | expectations.shift();
880 |
881 | if (expectation.response) {
882 | responses.push(function() {
883 | var response = expectation.response(method, url, data, headers);
884 | xhr.$$respHeaders = response[2];
885 | callback(response[0], response[1], xhr.getAllResponseHeaders());
886 | });
887 | return;
888 | }
889 | wasExpected = true;
890 | }
891 |
892 | var i = -1, definition;
893 | while ((definition = definitions[++i])) {
894 | if (definition.match(method, url, data, headers || {})) {
895 | if (definition.response) {
896 | // if $browser specified, we do auto flush all requests
897 | ($browser ? $browser.defer : responsesPush)(function() {
898 | var response = definition.response(method, url, data, headers);
899 | xhr.$$respHeaders = response[2];
900 | callback(response[0], response[1], xhr.getAllResponseHeaders());
901 | });
902 | } else if (definition.passThrough) {
903 | $delegate(method, url, data, callback, headers);
904 | } else throw Error('No response defined !');
905 | return;
906 | }
907 | }
908 | throw wasExpected ?
909 | Error('No response defined !') :
910 | Error('Unexpected request: ' + method + ' ' + url + '\n' +
911 | (expectation ? 'Expected ' + expectation : 'No more request expected'));
912 | }
913 |
914 | /**
915 | * @ngdoc method
916 | * @name ngMock.$httpBackend#when
917 | * @methodOf ngMock.$httpBackend
918 | * @description
919 | * Creates a new backend definition.
920 | *
921 | * @param {string} method HTTP method.
922 | * @param {string|RegExp} url HTTP url.
923 | * @param {(string|RegExp)=} data HTTP request body.
924 | * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
925 | * object and returns true if the headers match the current definition.
926 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
927 | * request is handled.
928 | *
929 | * - respond – `{function([status,] data[, headers])|function(function(method, url, data, headers)}`
930 | * – The respond method takes a set of static data to be returned or a function that can return
931 | * an array containing response status (number), response data (string) and response headers
932 | * (Object).
933 | */
934 | $httpBackend.when = function(method, url, data, headers) {
935 | var definition = new MockHttpExpectation(method, url, data, headers),
936 | chain = {
937 | respond: function(status, data, headers) {
938 | definition.response = createResponse(status, data, headers);
939 | }
940 | };
941 |
942 | if ($browser) {
943 | chain.passThrough = function() {
944 | definition.passThrough = true;
945 | };
946 | }
947 |
948 | definitions.push(definition);
949 | return chain;
950 | };
951 |
952 | /**
953 | * @ngdoc method
954 | * @name ngMock.$httpBackend#whenGET
955 | * @methodOf ngMock.$httpBackend
956 | * @description
957 | * Creates a new backend definition for GET requests. For more info see `when()`.
958 | *
959 | * @param {string|RegExp} url HTTP url.
960 | * @param {(Object|function(Object))=} headers HTTP headers.
961 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
962 | * request is handled.
963 | */
964 |
965 | /**
966 | * @ngdoc method
967 | * @name ngMock.$httpBackend#whenHEAD
968 | * @methodOf ngMock.$httpBackend
969 | * @description
970 | * Creates a new backend definition for HEAD requests. For more info see `when()`.
971 | *
972 | * @param {string|RegExp} url HTTP url.
973 | * @param {(Object|function(Object))=} headers HTTP headers.
974 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
975 | * request is handled.
976 | */
977 |
978 | /**
979 | * @ngdoc method
980 | * @name ngMock.$httpBackend#whenDELETE
981 | * @methodOf ngMock.$httpBackend
982 | * @description
983 | * Creates a new backend definition for DELETE requests. For more info see `when()`.
984 | *
985 | * @param {string|RegExp} url HTTP url.
986 | * @param {(Object|function(Object))=} headers HTTP headers.
987 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
988 | * request is handled.
989 | */
990 |
991 | /**
992 | * @ngdoc method
993 | * @name ngMock.$httpBackend#whenPOST
994 | * @methodOf ngMock.$httpBackend
995 | * @description
996 | * Creates a new backend definition for POST requests. For more info see `when()`.
997 | *
998 | * @param {string|RegExp} url HTTP url.
999 | * @param {(string|RegExp)=} data HTTP request body.
1000 | * @param {(Object|function(Object))=} headers HTTP headers.
1001 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1002 | * request is handled.
1003 | */
1004 |
1005 | /**
1006 | * @ngdoc method
1007 | * @name ngMock.$httpBackend#whenPUT
1008 | * @methodOf ngMock.$httpBackend
1009 | * @description
1010 | * Creates a new backend definition for PUT requests. For more info see `when()`.
1011 | *
1012 | * @param {string|RegExp} url HTTP url.
1013 | * @param {(string|RegExp)=} data HTTP request body.
1014 | * @param {(Object|function(Object))=} headers HTTP headers.
1015 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1016 | * request is handled.
1017 | */
1018 |
1019 | /**
1020 | * @ngdoc method
1021 | * @name ngMock.$httpBackend#whenJSONP
1022 | * @methodOf ngMock.$httpBackend
1023 | * @description
1024 | * Creates a new backend definition for JSONP requests. For more info see `when()`.
1025 | *
1026 | * @param {string|RegExp} url HTTP url.
1027 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1028 | * request is handled.
1029 | */
1030 | createShortMethods('when');
1031 |
1032 |
1033 | /**
1034 | * @ngdoc method
1035 | * @name ngMock.$httpBackend#expect
1036 | * @methodOf ngMock.$httpBackend
1037 | * @description
1038 | * Creates a new request expectation.
1039 | *
1040 | * @param {string} method HTTP method.
1041 | * @param {string|RegExp} url HTTP url.
1042 | * @param {(string|RegExp)=} data HTTP request body.
1043 | * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
1044 | * object and returns true if the headers match the current expectation.
1045 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1046 | * request is handled.
1047 | *
1048 | * - respond – `{function([status,] data[, headers])|function(function(method, url, data, headers)}`
1049 | * – The respond method takes a set of static data to be returned or a function that can return
1050 | * an array containing response status (number), response data (string) and response headers
1051 | * (Object).
1052 | */
1053 | $httpBackend.expect = function(method, url, data, headers) {
1054 | var expectation = new MockHttpExpectation(method, url, data, headers);
1055 | expectations.push(expectation);
1056 | return {
1057 | respond: function(status, data, headers) {
1058 | expectation.response = createResponse(status, data, headers);
1059 | }
1060 | };
1061 | };
1062 |
1063 |
1064 | /**
1065 | * @ngdoc method
1066 | * @name ngMock.$httpBackend#expectGET
1067 | * @methodOf ngMock.$httpBackend
1068 | * @description
1069 | * Creates a new request expectation for GET requests. For more info see `expect()`.
1070 | *
1071 | * @param {string|RegExp} url HTTP url.
1072 | * @param {Object=} headers HTTP headers.
1073 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1074 | * request is handled. See #expect for more info.
1075 | */
1076 |
1077 | /**
1078 | * @ngdoc method
1079 | * @name ngMock.$httpBackend#expectHEAD
1080 | * @methodOf ngMock.$httpBackend
1081 | * @description
1082 | * Creates a new request expectation for HEAD requests. For more info see `expect()`.
1083 | *
1084 | * @param {string|RegExp} url HTTP url.
1085 | * @param {Object=} headers HTTP headers.
1086 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1087 | * request is handled.
1088 | */
1089 |
1090 | /**
1091 | * @ngdoc method
1092 | * @name ngMock.$httpBackend#expectDELETE
1093 | * @methodOf ngMock.$httpBackend
1094 | * @description
1095 | * Creates a new request expectation for DELETE requests. For more info see `expect()`.
1096 | *
1097 | * @param {string|RegExp} url HTTP url.
1098 | * @param {Object=} headers HTTP headers.
1099 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1100 | * request is handled.
1101 | */
1102 |
1103 | /**
1104 | * @ngdoc method
1105 | * @name ngMock.$httpBackend#expectPOST
1106 | * @methodOf ngMock.$httpBackend
1107 | * @description
1108 | * Creates a new request expectation for POST requests. For more info see `expect()`.
1109 | *
1110 | * @param {string|RegExp} url HTTP url.
1111 | * @param {(string|RegExp)=} data HTTP request body.
1112 | * @param {Object=} headers HTTP headers.
1113 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1114 | * request is handled.
1115 | */
1116 |
1117 | /**
1118 | * @ngdoc method
1119 | * @name ngMock.$httpBackend#expectPUT
1120 | * @methodOf ngMock.$httpBackend
1121 | * @description
1122 | * Creates a new request expectation for PUT requests. For more info see `expect()`.
1123 | *
1124 | * @param {string|RegExp} url HTTP url.
1125 | * @param {(string|RegExp)=} data HTTP request body.
1126 | * @param {Object=} headers HTTP headers.
1127 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1128 | * request is handled.
1129 | */
1130 |
1131 | /**
1132 | * @ngdoc method
1133 | * @name ngMock.$httpBackend#expectPATCH
1134 | * @methodOf ngMock.$httpBackend
1135 | * @description
1136 | * Creates a new request expectation for PATCH requests. For more info see `expect()`.
1137 | *
1138 | * @param {string|RegExp} url HTTP url.
1139 | * @param {(string|RegExp)=} data HTTP request body.
1140 | * @param {Object=} headers HTTP headers.
1141 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1142 | * request is handled.
1143 | */
1144 |
1145 | /**
1146 | * @ngdoc method
1147 | * @name ngMock.$httpBackend#expectJSONP
1148 | * @methodOf ngMock.$httpBackend
1149 | * @description
1150 | * Creates a new request expectation for JSONP requests. For more info see `expect()`.
1151 | *
1152 | * @param {string|RegExp} url HTTP url.
1153 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1154 | * request is handled.
1155 | */
1156 | createShortMethods('expect');
1157 |
1158 |
1159 | /**
1160 | * @ngdoc method
1161 | * @name ngMock.$httpBackend#flush
1162 | * @methodOf ngMock.$httpBackend
1163 | * @description
1164 | * Flushes all pending requests using the trained responses.
1165 | *
1166 | * @param {number=} count Number of responses to flush (in the order they arrived). If undefined,
1167 | * all pending requests will be flushed. If there are no pending requests when the flush method
1168 | * is called an exception is thrown (as this typically a sign of programming error).
1169 | */
1170 | $httpBackend.flush = function(count) {
1171 | if (!responses.length) throw Error('No pending request to flush !');
1172 |
1173 | if (angular.isDefined(count)) {
1174 | while (count--) {
1175 | if (!responses.length) throw Error('No more pending request to flush !');
1176 | responses.shift()();
1177 | }
1178 | } else {
1179 | while (responses.length) {
1180 | responses.shift()();
1181 | }
1182 | }
1183 | $httpBackend.verifyNoOutstandingExpectation();
1184 | };
1185 |
1186 |
1187 | /**
1188 | * @ngdoc method
1189 | * @name ngMock.$httpBackend#verifyNoOutstandingExpectation
1190 | * @methodOf ngMock.$httpBackend
1191 | * @description
1192 | * Verifies that all of the requests defined via the `expect` api were made. If any of the
1193 | * requests were not made, verifyNoOutstandingExpectation throws an exception.
1194 | *
1195 | * Typically, you would call this method following each test case that asserts requests using an
1196 | * "afterEach" clause.
1197 | *
1198 | *