Karma testing gives "object is undefined" error in yeoman

I am trying unit test controllers inside a project that is scaffolded from yeoman. Running " karma start test/karma.conf.js" is giving me the error TypeError: ‘undefined’ is not an object (evaluating ‘scope.hi’)…

Here is my test file:

describe('Controller:JobsCtrl', function () {

  // load the controller's module
  beforeEach(module('myApp','myApp.controllers'));

  var JobsCtrl,
    scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();
    JobsCtrl = $controller('JobsCtrl', {
      $scope: scope
    });
  }));

  window.scope = scope;
  it('scope hi should be hi', function () {
    expect(scope.hi).toBe('jobs');
  });
});

One of my controllers looks like this:

angular.module('myApp')
  .controller('JobsCtrl', ['$scope',function ($scope) {
   	
    $scope.hi = "hello";

}]);

and my karma.conf.js file partially looks like this;

 config.set({
    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // base path, that will be used to resolve files and exclude
    basePath: '../',

    // testing framework to use (jasmine/mocha/qunit/...)
    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
      'bower_components/angular/angular.js',
      'bower_components/angular-mocks/angular-mocks.js',
      'bower_components/angular-animate/angular-animate.js',
      'bower_components/angular-cookies/angular-cookies.js',
      'bower_components/angular-resource/angular-resource.js',
      'bower_components/angular-route/angular-route.js',
      'bower_components/angular-sanitize/angular-sanitize.js',
      'bower_components/angular-touch/angular-touch.js',
      'app/scripts/*.js',
      'app/scripts/**/*.js',
      'test/spec/**/*.js'

       ],
    ...
 });
};

And this is my bower.json file partially:

{
  "name": "myApp",
  "version": "0.0.0",
  "dependencies": {
    "angular": "~1.2.9",
     ....
    "bootstrap": "^3.2.0",
    "angular-mocks": "1.2.28",
     ....
  },
  "devDependencies": {
    "angular-mocks": "1.2.28",
    "angular-scenario": "~1.3.0",
    "karma": "~0.4.6",
    "karma-jasmine": "~0.3.5"
  },
  "appPath": "app",
  "resolutions": {
    "angular": ">=1 <1.3.0",
    "jquery": "~2.1.3",
    "angular-animate": "1.2.28",
    "angular-route": "1.2.28",
    "angular-mocks": "1.2.28"
  }
}

I have searched online for possible solutions, but none of them were helpful in solving this particular issue. Can anyone tell me whats wrong?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.