web staff / log -> getting started w/ testing
authorBill Erickson <berick@esilibrary.com>
Tue, 26 Nov 2013 15:31:24 +0000 (10:31 -0500)
committerBill Erickson <berick@esilibrary.com>
Tue, 26 Nov 2013 15:31:24 +0000 (10:31 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
web-staff-log.txt

index 2e61630..d8a91be 100644 (file)
@@ -332,6 +332,109 @@ highly recommend Chrome's "Disable Cache (while dev tools is open)" option
 found under the Javascript console (dev tools) configuration menu (gear
 icon, bottom right) since Chrome aggressively caches the templates.
 
+2013-11-26 Getting Started with Testing
+---------------------------------------
+
+Today I wrote one unit test for a stub controller from 
+https://github.com/angular/angular-seed[angular-seed].
+Here's what I did.  I'm still figuring this out, so bear with me...
+
+ * On my desktop, I installed node.js and npm (node package manager) and
+the node plugin 'karma'.
+
+[source,sh]
+-----------------------------------------------------------------------------
+% sudo apt-get install nodejs npm
+# node.js is installed at 'nodejs' -- npm, etc. assume 'node'
+% sudo ln -s /usr/bin/nodejs /usr/bin/node
+% sudo npm install -g karma
+-----------------------------------------------------------------------------
+
+ * Clone the angular-seed repository, which provides a stub test environment
+
+[source,sh]
+-----------------------------------------------------------------------------
+% git clone https://github.com/angular/angular-seed
+-----------------------------------------------------------------------------
+
+ * Modify the angular-seed test script, which makes some odd assumptions
+   about the location of binaries
+
+[source,diff]
+-----------------------------------------------------------------------------
+diff --git a/scripts/test.sh b/scripts/test.sh
+index 972001f..f6db762 100755
+--- a/scripts/test.sh
++++ b/scripts/test.sh
+@@ -6,4 +6,5 @@ echo ""
+echo "Starting Karma Server (http://karma-runner.github.io)"
+echo "-------------------------------------------------------------------"
+   
+-$BASE_DIR/../node_modules/karma/bin/karma start $BASE_DIR/../config/karma.conf.js $*
++#$BASE_DIR/../node_modules/karma/bin/karma start $BASE_DIR/../config/karma.conf.js $*
++karma start $BASE_DIR/../config/karma.conf.js $*
+-----------------------------------------------------------------------------
+
+ * Modify the stock controller and controller unit test to do something
+   (very simple).
+
+[source,diff]
+-----------------------------------------------------------------------------
+diff --git a/app/js/controllers.js b/app/js/controllers.js
+index cc9d305..679570d 100644
+--- a/app/js/controllers.js
++++ b/app/js/controllers.js
+@@ -3,9 +3,10 @@
+ /* Controllers */
+ angular.module('myApp.controllers', []).
+-  controller('MyCtrl1', [function() {
++  controller('MyCtrl1', ['$scope', function($scope) {
++    $scope.foo = 'hello';
+   }])
+diff --git a/test/unit/controllersSpec.js b/test/unit/controllersSpec.js
+index 23f6b09..ec741a6 100644
+--- a/test/unit/controllersSpec.js
++++ b/test/unit/controllersSpec.js
+@@ -3,11 +3,18 @@
+ /* jasmine specs for controllers go here */
+ describe('controllers', function(){
+  beforeEach(module('myApp.controllers'));
++    // create the scope, instantiate the controller
++    var ctrl, scope;
++    beforeEach(inject(function ($rootScope, $controller) {
++        scope = $rootScope.$new();
++        ctrl = $controller('MyCtrl1', {$scope: scope});
++    }));
+   it('should ....', inject(function() {
+     //spec body
++    expect(scope.foo).toEqual('hello');
+   }));
+-----------------------------------------------------------------------------
+
+ * Launched the test, which fires up Chrome and logs the output to the 
+   terminal
+
+[source,sh]
+-----------------------------------------------------------------------------
+CHROME_BIN=/usr/bin/chromium-browser scripts/test.sh
+
+Starting Karma Server (http://karma-runner.github.io)
+INFO [karma]: Karma v0.10.5 server started at http://localhost:9876/
+INFO [launcher]: Starting browser Chrome
+INFO [Chromium 30.0.1599 (Ubuntu)]: Connected on socket 344641UsyCbWCVxk_28H
+Chromium 30.0.1599 (Ubuntu): Executed 5 of 5 SUCCESS (0.612 secs / 0.088 secs)
+-----------------------------------------------------------------------------
+
+All 5 tests succeeded, even my rigorous $scope.foo='hello' test.  
+
+Next steps for testing will be to create an environment around the staff
+client code so we can write some real tests...
+
 Future Topics...
 ----------------
 
@@ -340,6 +443,7 @@ Future Topics...
  * Deep Linking / Managing the _first load_ problem
  * When to use Angular Templates vs Template Toolkit Templates
  * Displaying bib records in the prototype
+ * More testing
 
 
 ////