2

I am playing around with https://github.com/angular/angular-seed

A controller is defined in app/controllers.js like this

'use strict';
function MyCtrl1() {}
MyCtrl1.$inject = [];

this doesn't pass jshint as MyCtrl1 is referenced in app/app.js and not in my globals list.

According to Brian Ford and others I have read the preferred style is

angular.module('myApp').controller('MyCtrl1', [], function () {});

I like this better as it's not in the global scope, but now my testacular specs fail because this doesn't work anymore:

var myCtrl1;
beforeEach(function(){
  myCtrl1 = new MyCtrl1();
});

How do I get a reference to this controller which is defined in the "preferred" style for testing purposes?

1
  • 1
    This might be the same issue that I have anwsered here You need to add beforeEach(module('myApp')); Commented Apr 8, 2013 at 0:05

2 Answers 2

3

credit due to both Javito and Xesued:

beforeEach(module('myApp'));
var scope, ctrl;
beforeEach(inject(function($controller, $rootScope) {
    scope = $rootScope.$new();
    ctrl = $controller('MyCtrl1', {$scope: scope});
}));
Sign up to request clarification or add additional context in comments.

Comments

0

Try,

beforeEach(inject(function($controller) {
    scope = {};    
    MyCtrl1 = $controller('MyCtrl1', {
      $scope: scope
    });
  }));

1 Comment

close, it seems you need a real scope, and also the beforeEach as suggested by Xesued in the comments. code is not working out here, I guess I have to answer it myself. Sorry, I wanted to give you the credit :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.