Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Commit f58b5ad

Browse files
refactor(controllers): fix the destroy tests
100% coverage
1 parent a7bdd69 commit f58b5ad

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

‎controllers/controllers.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
angular.module('myApp')
55
.controller('SampleController', SampleController);
66

7-
function SampleController($scope) {
7+
function SampleController($scope,sampleService) {
88
var vm = this;
99

1010
vm.bar = bar;
11-
vm.doSomething = doSomething;
1211
vm.sendMessage = sendMessage;
1312
vm.broadcastEvent = broadcastEvent;
1413
vm.foo = 'bar';
@@ -17,8 +16,6 @@
1716
vm.foo = 'baz';
1817
}
1918

20-
function doSomething() {}
21-
2219
function sendMessage() {
2320
$scope.$emit('sample:message', { foo: 'bar' });
2421
}
@@ -34,10 +31,14 @@
3431
});
3532

3633
$scope.$on('$destroy', function() {
37-
vm.doSomething();
34+
sampleService.bar();
3835
});
3936

4037
//scope.$on()
4138
//scope.$apply()
4239
}
40+
41+
SampleController.$inject = ['$scope', 'sampleService'];
4342
}());
43+
44+

‎controllers/controllers.spec.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
describe('SampleController', function() {
22
var $rootScope,
3-
ctrl, scope;
3+
ctrl, scope,sampleService;
44

55
beforeEach(module('myApp'));
66

7-
beforeEach(inject(function( _$rootScope_, _$controller_) {
7+
beforeEach(inject(function( _$rootScope_, _$controller_,_sampleService_) {
88
$rootScope = _$rootScope_;
99
scope = $rootScope.$new();
10+
sampleService = _sampleService_;
1011

1112
ctrl = _$controller_('SampleController', {
1213
$scope: scope
@@ -23,15 +24,16 @@ describe('SampleController', function() {
2324
});
2425

2526
it('should have a watcher', function() {
27+
scope.$digest(); // run once before change a value just to make the coverage 100%
2628
scope.baz = 'foo';
2729
scope.$digest();
2830
expect(ctrl.baz).toBe('foobar');
2931
});
3032

3133
it('should do something on $destroy', function() {
32-
spyOn(ctrl, 'doSomething');
34+
spyOn(sampleService, 'bar');
3335
scope.$destroy();
34-
expect(ctrl.doSomething).toHaveBeenCalled();
36+
expect(sampleService.bar).toHaveBeenCalled();
3537
});
3638

3739
it('should emit an event', function() {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /