@@ -133,19 +137,19 @@ angular.module('myModule', []).
// This is an example of config block.
// You can have as many of these as you want.
// You can only inject Providers (not instances)
- // into the config blocks.
+ // into config blocks.
}).
run(function(injectables) { // instance-injector
// This is an example of a run block.
// You can have as many of these as you want.
// You can only inject instances (not Providers)
- // into the run blocks
+ // into run blocks
});
```
## Configuration Blocks
-There are some convenience methods on the module which are equivalent to the config block. For
+There are some convenience methods on the module which are equivalent to the `config` block. For
example:
```js
@@ -166,8 +170,10 @@ angular.module('myModule', []).
});
```
-The configuration blocks get applied in the order in which they are registered. The only exception
-to it are constant definitions, which are placed at the beginning of all configuration blocks.
+
+When bootstrapping, first Angular applies all constant definitions.
+Then Angular applies configuration blocks in the order same order they were registered.
+
## Run Blocks
@@ -198,72 +204,73 @@ Beware that using `angular.module('myModule', [])` will create the module `myMod
existing module named `myModule`. Use `angular.module('myModule')` to retrieve an existing module.
```js
- var myModule = angular.module('myModule', []);
-
- // add some directives and services
- myModule.service('myService', ...);
- myModule.directive('myDirective', ...);
+var myModule = angular.module('myModule', []);
+
+// add some directives and services
+myModule.service('myService', ...);
+myModule.directive('myDirective', ...);
- // overwrites both myService and myDirective by creating a new module
- var myModule = angular.module('myModule', []);
+// overwrites both myService and myDirective by creating a new module
+var myModule = angular.module('myModule', []);
- // throws an error because myOtherModule has yet to be defined
- var myModule = angular.module('myOtherModule');
+// throws an error because myOtherModule has yet to be defined
+var myModule = angular.module('myOtherModule');
```
# Unit Testing
-In its simplest form a unit test is a way of instantiating a subset of the application in test and
-then applying a stimulus to it. It is important to realize that each module can only be loaded
-once per injector. Typically an app has only one injector. But in tests, each test has its own
-injector, which means that the modules are loaded multiple times per VM. Properly structured
-modules can help with unit testing, as in this example:
+A unit test is a way of instantiating a subset of an application to apply stimulus to it.
+Small, structured modules help keep unit tests concise and focused.
+
+
+Each module can only be loaded once per injector.
+Usually an Angular app has only one injector and modules are only loaded once.
+Each test has its own injector and modules are loaded multiple times.
+
In all of these examples we are going to assume this module definition:
```js
- angular.module('greetMod', []).
+angular.module('greetMod', []).
- factory('alert', function($window) {
- return function(text) {
- $window.alert(text);
- }
- }).
+ factory('alert', function($window) {
+ return function(text) {
+ $window.alert(text);
+ }
+ }).
- value('salutation', 'Hello').
+ value('salutation', 'Hello').
- factory('greet', function(alert, salutation) {
- return function(name) {
- alert(salutation + ' ' + name + '!');
- }
- });
+ factory('greet', function(alert, salutation) {
+ return function(name) {
+ alert(salutation + ' ' + name + '!');
+ }
+ });
```
-Let's write some tests:
+Let's write some tests to show how to override configuration in tests.
```js
describe('myApp', function() {
- // load the relevant application modules then load a special
- // test module which overrides the $window with a mock version,
- // so that calling window.alert() will not block the test
- // runner with a real alert box. This is an example of overriding
- // configuration information in tests.
+ // load application module (`greetMod`) then load a special
+ // test module which overrides `$window` with a mock version,
+ // so that calling `window.alert()` will not block the test
+ // runner with a real alert box.
beforeEach(module('greetMod', function($provide) {
$provide.value('$window', {
alert: jasmine.createSpy('alert')
});
}));
- // The inject() will create the injector and inject the greet and
- // $window into the tests. The test need not concern itself with
- // wiring of the application, only with testing it.
+ // inject() will create the injector and inject the `greet` and
+ // `$window` into the tests.
it('should alert on $window', inject(function(greet, $window) {
greet('World');
expect($window.alert).toHaveBeenCalledWith('Hello World!');
}));
// this is another way of overriding configuration in the
- // tests using an inline module and inject methods.
+ // tests using inline `module` and `inject` methods.
it('should alert using the alert service', function() {
var alertSpy = jasmine.createSpy('alert');
module(function($provide) {
--
cgit v1.2.3