Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have 2 unit tests in the example (2 <code>it</code> blocks). They look like they are supposed to do the same thing, but only the second actually creates your controller.</p> <p>When you defined the controller in angular, it is not a globally available object that you can initialise with <code>new Controller(...)</code>. You must request it from angular.</p> <p>Your second test (which seems to be passing) does this by injecting the <code>$controller</code> service which will do perform any actions needed to set up and request the controller.</p> <pre><code>inject(function($controller) { ... }); </code></pre> <p>It then creates the controller using this service like so</p> <pre><code>var scope = {}, ctrl = $controller('PhoneListCtrl', {$scope:scope}); </code></pre> <p>In your first test you try to just use the <code>PhoneListCtrl</code> variable directly. As the error says, this doesn't exist unless you define a variable with that name in your function.</p> <hr> <p>I have just noticed that failing test in the tutorial. This is specifically for if you have defined a controller on the global namespace. For example</p> <pre><code>function PhoneListCtrl($scope) { $scope.phones = [ {'name': 'Nexus S', 'snippet': 'Fast just got faster with Nexus S.'}, {'name': 'Motorola XOOM™ with Wi-Fi', 'snippet': 'The Next, Next Generation tablet.'}, {'name': 'MOTOROLA XOOM™', 'snippet': 'The Next, Next Generation tablet.'} ]; $scope.hello = "Hello World"; }; phonecatApp.controller('PhoneListCtrl', PhoneListCtrl); </code></pre> <p>The test would then work because you have function defined globally that is used as a controller, so you are able to test it without paying attention to the fact it is a controller. This means if you try to use other services you will have to inject them yourself and perform any actions that <code>$controller</code> would do for you.</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