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

Commit 46a5b2c

Browse files
committed
docs(ngMockE2E): add $httpBackend mock example
1 parent 28e4707 commit 46a5b2c

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

src/ngMock/angular-mocks.js

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

23332399
/**

0 commit comments

Comments
 (0)