Skip to content

Commit 9dd2a16

Browse files
committed
Add Unit testing section
1 parent 5394762 commit 9dd2a16

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,81 @@ _Why_: Using DI makes testing and refactoring easier.
811811

812812
_Why_: You should `$scope.$apply()` as close to the asynchronous event binding as possible.
813813

814+
### Testing
815+
816+
Our applications are covered by two different types of test:
817+
- unit tests, which test individual components by asserting that they behave as expected.
818+
- End to End, or E2E, tests, which load up the application in a browser and interact with it as if a user would, asserting the application behaves expectedly.
819+
820+
To write our tests we use [Jasmine BDD](http://jasmine.github.io/2.0/introduction.html) and [ngMock](https://docs.angularjs.org/api/ngMock).
821+
822+
#### Unit Testing
823+
824+
Every component should have a comprehensive set of unit tests.
825+
826+
##### Structure of Unit Tests
827+
828+
Tests should be grouped into logical blocks using Jasmine's `describe` function. Tests for a function should all be contained within a `describe` block, and `describe` blocks should also be used to describe different scenarios, or _contexts_:
829+
830+
```js
831+
describe('#update', function() {
832+
describe('when the data is valid', function() {
833+
it('shows the success message', function() {…});
834+
});
835+
836+
describe('when the data is invalid', function() {
837+
it('shows errors', function() {…});
838+
});
839+
});
840+
```
841+
842+
##### Dependencies
843+
844+
Each component should have its dependencies stubbed in each test.
845+
846+
Inject the dependencies and the components being tested in a `beforeEach` function. This encapsulates each test's state, ensuring that they are independent, making them easier to reason about. Tests should never depend on being run in a specific order.
847+
848+
```js
849+
var SomeService;
850+
851+
beforeEach(inject(function($injector) {
852+
SomeService = $injector.get('SomeService');
853+
}));
854+
```
855+
856+
##### Controllers
857+
858+
When injecting controllers for a test, use the `controller as` syntax:
859+
860+
```js
861+
beforeEach(inject(function($injector, $controller) {
862+
$controller('OrganisationController as ctrl', {…});
863+
}));
864+
```
865+
866+
Always create a new scope to pass into the controller:
867+
868+
```js
869+
var scope;
870+
beforeEach(inject(function($injector, $controller) {
871+
scope = $injector.get('$rootScope').$new();
872+
$controller('OrganisationController as ctrl', {
873+
$scope: scope,
874+
875+
});
876+
}));
877+
```
878+
879+
##### Fixtures
880+
881+
When stubbing an API request using `$httpBackend`, always respond with a correctly formatted object. These responses should be saved individually as `.json` files and imported using the SystemJS JSON plugin:
882+
883+
```js
884+
import updateFixture from 'app/services/roles/update.fixture.json!json
885+
886+
$httpBackend.expectPUT('someurl.com').respond(201, updateFixture);
887+
```
888+
814889
## Credits
815890
816891
We referred to lots of resources during the creation of this styleguide, including:

0 commit comments

Comments
 (0)