javascriptangularjskarma-jasmineangular-seed

When adding scope to the Angular-Seed default View Controllers the test suite now fails


Using a fresh clone of angular-seed I am attempting some BDD and have added the following tests and code. However, once I add the $scope to the controller, the suite fails on the expect(view1Ctrl).toBeDefined(); expectation.

Below is the only addition I've made and it causes the noted failure when Karma runs.

app/view1/view1.js

.controller('View1Ctrl', ['$scope', function($scope) {
  $scope.name = "Name";
}]);

Solution

  • in your test (view1_test.js) you need to inject $scope into the controller...

        describe('myApp.view1 module', function() {
    
      beforeEach(module('myApp.view1'));
    
      describe('view1 controller', function(){
    
        it('should ....', inject(function($controller, $rootScope) {
          //spec body
          var $scope = $rootScope.$new();
          var view1Ctrl = $controller('View1Ctrl', {$scope: $scope});
          expect(view1Ctrl).toBeDefined();
        }));
    
      });
    });