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

Commit 17ba2a6

Browse files
committed
docs(guide/Unit Testing): add info about promises
Related #1915
1 parent 3bfeda3 commit 17ba2a6

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

docs/content/guide/unit-testing.ngdoc

+8
Original file line numberDiff line numberDiff line change
@@ -430,5 +430,13 @@ If your directive uses `templateUrl`, consider using
430430
to pre-compile HTML templates and thus avoid having to load them over HTTP during test execution.
431431
Otherwise you may run into issues if the test directory hierarchy differs from the application's.
432432

433+
## Testing Promises
434+
435+
When testing promises, it's important to know that the resolution of promises is tied to the {@link ng.$rootScope.Scope#$digest digest cycle}.
436+
That means a promise's `then`, `catch` and `finally` callback functions are only called after a digest has run.
437+
In tests, you can trigger a digest by calling a scope's {@link ng.$rootScope.Scope#$apply `$apply` function}.
438+
If you don't have a scope in your test, you can inject the {@link ng.$rootScope $rootScope} and call `$apply` on it.
439+
There is also an example of testing promises in the {@link ng.$q#testing `$q` service documentation}.
440+
433441
## Sample project
434442
See the [angular-seed](https://github.com/angular/angular-seed) project for an example.

src/ng/q.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
* - Q has many more features than $q, but that comes at a cost of bytes. $q is tiny, but contains
187187
* all the important functionality needed for common async tasks.
188188
*
189-
* # Testing
189+
* # Testing
190190
*
191191
* ```js
192192
* it('should simulate promise', inject(function($q, $rootScope) {

0 commit comments

Comments
 (0)