Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The main problem with your test code is that it tries to create a controller's instance "by hand" using the new operator. When doing so AngularJS has no chance to inject dependencies. What you should be doing is to allow AngularJS inject dependencies:</p> <pre><code>var $scope, ctrl; //you need to inject dependencies first beforeEach(inject(function($rootScope) { $scope = $rootScope.$new(); })); it('Should initialize value to Loading', inject(function($controller) { ctrl = $controller('MainNavController', { $scope: $scope }); expect($scope.wksp_name).toBe('Loading...'); })); </code></pre> <p>Here is the link to a complete jsFiddle: <a href="http://jsfiddle.net/pkozlowski_opensource/7a7KR/3/" rel="noreferrer">http://jsfiddle.net/pkozlowski_opensource/7a7KR/3/</a></p> <p>There are 2 things worth noting in the above example:</p> <ol> <li>You can use the inject() method from the ngMock module to inject dependencies: <a href="https://docs.angularjs.org/api/ngMock/function/angular.mock.inject" rel="noreferrer">https://docs.angularjs.org/api/ngMock/function/angular.mock.inject</a></li> <li>To create a controller instance (that supports dependency injection) you would use the $controller service: <a href="http://docs.angularjs.org/api/ng.$controller" rel="noreferrer">http://docs.angularjs.org/api/ng.$controller</a></li> </ol> <p>As the last remark: I would advise naming controllers starting with an uppercase letter - this way we won't confuse them with variable names.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload