Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Commit 6874e71

Browse files
committed
feat(controller): add the explanation
- add the README.md - refactor to use an app.js
1 parent 1044655 commit 6874e71

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Even with AngularJS providing us a pretty testable way of work, we still need so
55

66
## Table of contents
77

8-
- [Controllers](https://github.com/cironunes/angular-testing-recipes/blob/master/src/controller.js)
8+
- [Controllers](https://github.com/cironunes/angular-testing-recipes/tree/master/controller)
99
- Services
1010
- Filters
1111
- Directives

app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('myApp', []);
5+
}());

controller/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Controller
2+
> Testing recipes for controllers
3+
4+
## Table of contents
5+
6+
- [Boilerplate](#boilerplate)
7+
- [Expose properties](#expose-properties)
8+
- [Expose methods](#expose-methods)
9+
- [Watchers](#watchers)
10+
- [$destroy](#$destroy)
11+
- $emit
12+
- $broadcast
13+
- $on
14+
- $apply
15+
16+
## Boilerplate
17+
18+
Before start we need to initialize the related controller and mock it's dependencies.
19+
20+
```js
21+
describe('SampleController', function() {
22+
'use strict';
23+
24+
var $rootScope,
25+
ctrl, scope;
26+
27+
// load the related module before each spec runs
28+
beforeEach(module('moduleName'));
29+
30+
// inject the $controller service to load the SampleController
31+
// and the $rootScope
32+
beforeEach(inject(function(_$rootScope_, _$controller_) {
33+
// save the injected $rootScope into a variable available across the whole file
34+
$rootScope = _$rootScope_;
35+
36+
// create a brand new scope
37+
scope = $rootScope.$new();
38+
39+
// load the SampleController mocking the $scope to the one created before
40+
ctrl = $controller('SampleController', {
41+
$scope: scope
42+
};
43+
}));
44+
45+
...
46+
});
47+
```
48+
49+
## Expose properties
50+
51+
## Expose methods
52+
53+
## Watchers
54+
55+
## $destroy
56+

controller/controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function() {
22
'use strict';
33

4-
angular.module('myApp', [])
4+
angular.module('myApp')
55
.controller('SampleController', SampleController);
66

77
function SampleController($scope) {

karma.conf.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module.exports = function(config) {
1818
'bower_components/angular/angular.js',
1919
'bower_components/angular-mocks/angular-mocks.js',
2020

21+
'app.js',
22+
2123
'controller/controller.js',
2224

2325
'controller/controller.spec.js'

0 commit comments

Comments
 (0)