Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 997f482

Browse files
committed
docs(ngMockE2E): add $httpBackend mock example
1 parent 0cb94ed commit 997f482

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

src/ngMock/angular-mocks.js

+71-5
Original file line numberDiff line numberDiff line change
@@ -997,8 +997,10 @@ angular.mock.dump = function(object) {
997997
* Fake HTTP backend implementation suitable for unit testing applications that use the
998998
* {@link ng.$http $http service}.
999999
*
1000-
* *Note*: For fake HTTP backend implementation suitable for end-to-end testing or backend-less
1000+
* <div class="alert alert-info">
1001+
* **Note**: For fake HTTP backend implementation suitable for end-to-end testing or backend-less
10011002
* development please see {@link ngMockE2E.$httpBackend e2e $httpBackend mock}.
1003+
* </div>
10021004
*
10031005
* During unit testing, we want our unit tests to run quickly and have no external dependencies so
10041006
* we don’t want to send [XHR](https://developer.mozilla.org/en/xmlhttprequest) or
@@ -2288,8 +2290,10 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
22882290
* Fake HTTP backend implementation suitable for end-to-end testing or backend-less development of
22892291
* applications that use the {@link ng.$http $http service}.
22902292
*
2291-
* *Note*: For fake http backend implementation suitable for unit testing please see
2293+
* <div class="alert alert-info">
2294+
* **Note**: For fake http backend implementation suitable for unit testing please see
22922295
* {@link ngMock.$httpBackend unit-testing $httpBackend mock}.
2296+
* </div>
22932297
*
22942298
* This implementation can be used to respond with static or dynamic responses via the `when` api
22952299
* and its shortcuts (`whenGET`, `whenPOST`, etc) and optionally pass through requests to the
@@ -2310,9 +2314,9 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
23102314
* on the `ngMockE2E` and your application modules and defines the fake backend:
23112315
*
23122316
* ```js
2313-
* myAppDev = angular.module('myAppDev', ['myApp', 'ngMockE2E']);
2317+
* var myAppDev = angular.module('myAppDev', ['myApp', 'ngMockE2E']);
23142318
* myAppDev.run(function($httpBackend) {
2315-
* phones = [{name: 'phone1'}, {name: 'phone2'}];
2319+
* var phones = [{name: 'phone1'}, {name: 'phone2'}];
23162320
*
23172321
* // returns the current list of phones
23182322
* $httpBackend.whenGET('/phones').respond(phones);
@@ -2323,12 +2327,74 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
23232327
* phones.push(phone);
23242328
* return [200, phone, {}];
23252329
* });
2326-
* $httpBackend.whenGET(/^\/templates\//).passThrough();
2330+
* $httpBackend.whenGET(/^\/templates\//).passThrough(); // Requests for templare are handled by the real server
23272331
* //...
23282332
* });
23292333
* ```
23302334
*
23312335
* Afterwards, bootstrap your app with this new module.
2336+
*
2337+
* ## Example
2338+
* <example name="httpbackend-e2e-testing" module="myAppE2E" deps="angular-mocks.js">
2339+
* <file name="app.js">
2340+
* var myApp = angular.module('myApp', []);
2341+
*
2342+
* myApp.controller('main', function($http) {
2343+
* var ctrl = this;
2344+
*
2345+
* ctrl.phones = [];
2346+
* ctrl.newPhone = {
2347+
* name: ''
2348+
* };
2349+
*
2350+
* ctrl.getPhones = function() {
2351+
* $http.get('/phones').then(function(response) {
2352+
* ctrl.phones = response.data;
2353+
* });
2354+
* };
2355+
*
2356+
* ctrl.addPhone = function(phone) {
2357+
* $http.post('/phones', phone).then(function() {
2358+
* ctrl.newPhone = {name: ''};
2359+
* return ctrl.getPhones();
2360+
* });
2361+
* };
2362+
*
2363+
* ctrl.getPhones();
2364+
* });
2365+
* </file>
2366+
* <file name="e2e.js">
2367+
* var myAppDev = angular.module('myAppE2E', ['myApp', 'ngMockE2E']);
2368+
*
2369+
* myAppDev.run(function($httpBackend) {
2370+
* var phones = [{name: 'phone1'}, {name: 'phone2'}];
2371+
*
2372+
* // returns the current list of phones
2373+
* $httpBackend.whenGET('/phones').respond(phones);
2374+
*
2375+
* // adds a new phone to the phones array
2376+
* $httpBackend.whenPOST('/phones').respond(function(method, url, data) {
2377+
* var phone = angular.fromJson(data);
2378+
* phones.push(phone);
2379+
* return [200, phone, {}];
2380+
* });
2381+
* });
2382+
* </file>
2383+
* <file name="index.html">
2384+
* <div ng-controller="main as $ctrl">
2385+
* <form name="newPhoneForm" ng-submit="$ctrl.addPhone($ctrl.newPhone)">
2386+
* <input type="text" ng-model="$ctrl.newPhone.name">
2387+
* <input type="submit" value="Add Phone">
2388+
* </form>
2389+
* <h1>Phones</h1>
2390+
* <ul>
2391+
* <li ng-repeat="phone in $ctrl.phones">{{phone.name}}</li>
2392+
* </ul>
2393+
* </div>
2394+
* </file>
2395+
* </example>
2396+
*
2397+
*
23322398
*/
23332399

23342400
/**

0 commit comments

Comments
 (0)