From 8013fa8543dc769ae35ac714703a7fcc67a0fb1f Mon Sep 17 00:00:00 2001 From: Will Ware Date: Fri, 3 May 2013 18:52:58 -0300 Subject: [PATCH 1/3] Show how to test a method in a unit test --- ...v_guide.mvc.understanding_controller.ngdoc | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc b/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc index cb4acef7f21e..cda61f5b4e4f 100644 --- a/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc +++ b/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc @@ -283,6 +283,59 @@ describe('state', function() { }); +Functions or methods in a controller can be tested as well. Rather than take arguments or return values +directly, they are more likely to read and write attributes on the scope, as shown below, because that's +compatible with how data binding works. + +
+function PasswordCtrl($scope) {
+    $scope.password = '';
+    $scope.grade = function() {
+        var size = $scope.password.length;
+        if (size > 8) {
+            $scope.strength = 'strong';
+        } else if (size > 3) {
+            $scope.strength = 'medium';
+        } else {
+            $scope.strength = 'weak';
+        }
+    }
+}
+
+ +and the test is straight forward + +
+describe('myController function', function() {
+
+    describe('myController', function() {
+        var scope, ctrl;
+
+        beforeEach(inject(function($rootScope, $controller) {
+            scope = $rootScope.$new();
+            $controller(PasswordCtrl, {$scope: scope});
+        }));
+
+        it('should grade short passwords as weak', function() {
+            scope.password = 'abc';
+            scope.grade();
+            expect(scope.strength).toEqual('weak');
+        });
+
+        it('should grade long passwords as strong', function() {
+            scope.password = 'abcdefghijklm';
+            scope.grade();
+            expect(scope.strength).toEqual('strong');
+        });
+    });
+});
+
+ +Notice that the test is not only much shorter but it is easier to follow what is going on. We say +that such a test tells a story, rather then asserting random bits which don't seem to be related. + + + ## Related Topics From 734963fe46b9d8f08751f4be6e76af4f476cac7f Mon Sep 17 00:00:00 2001 From: Will Ware Date: Fri, 3 May 2013 18:55:05 -0300 Subject: [PATCH 2/3] remove unnecessary variable --- docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc b/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc index cda61f5b4e4f..4717493a33fd 100644 --- a/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc +++ b/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc @@ -309,7 +309,7 @@ and the test is straight forward describe('myController function', function() { describe('myController', function() { - var scope, ctrl; + var scope; beforeEach(inject(function($rootScope, $controller) { scope = $rootScope.$new(); From 64dcde314951f93d46d3420e166c0143778f7305 Mon Sep 17 00:00:00 2001 From: Will Ware Date: Fri, 3 May 2013 18:58:25 -0300 Subject: [PATCH 3/3] better name for test?? --- .../guide/dev_guide.mvc.understanding_controller.ngdoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc b/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc index 4717493a33fd..9a74e363655e 100644 --- a/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc +++ b/docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc @@ -306,9 +306,9 @@ function PasswordCtrl($scope) { and the test is straight forward
-describe('myController function', function() {
+describe('PasswordCtrl test', function() {
 
-    describe('myController', function() {
+    describe('PasswordCtrlTest', function() {
         var scope;
 
         beforeEach(inject(function($rootScope, $controller) {