UNPKG

knowhow-server

Version:

A personal workflow engine that can manage and use any network connected host

78 lines (65 loc) 3.98 kB
<a href="http://github.com/angular/angular.js/tree/v1.2.9/src/ngMock/angular-mocks.js#L2027" class="view-source btn btn-action"><i class="icon-zoom-in"> </i> View source</a><a href="http://github.com/angular/angular.js/edit/master/src/ngMock/angular-mocks.js" class="improve-docs btn btn-primary"><i class="icon-edit"> </i> Improve this doc</a><h1><code ng:non-bindable="">angular.mock.inject</code> <div><span class="hint">API in module <code ng:non-bindable="">ng</code> </span> </div> </h1> <div><h2 id="description">Description</h2> <div class="description"><div class="angular-mock-inject-page"><p><em>NOTE</em>: This function is also published on window for easy access.<br></p> <p>The inject function wraps a function into an injectable function. The inject() creates new instance of <a href="api/AUTO.$injector"><code>$injector</code></a> per test, which is then used for resolving references.</p> <h4 id="description_resolving-references">Resolving References (Underscore Wrapping)</h4> <p>Often, we would like to inject a reference once, in a <code>beforeEach()</code> block and reuse this in multiple <code>it()</code> clauses. To be able to do this we must assign the reference to a variable that is declared in the scope of the <code>describe()</code> block. Since we would, most likely, want the variable to have the same name of the reference we have a problem, since the parameter to the <code>inject()</code> function would hide the outer variable.</p> <p>To help with this, the injected parameters can, optionally, be enclosed with underscores. These are ignored by the injector when the reference name is resolved.</p> <p>For example, the parameter <code>_myService_</code> would be resolved as the reference <code>myService</code>. Since it is available in the function body as <em>myService</em>, we can then assign it to a variable defined in an outer scope.</p> <pre><code>// Defined out reference variable outside var myService; // Wrap the parameter in underscores beforeEach( inject( function(_myService_){ myService = _myService_; })); // Use myService in a series of tests. it(&#39;makes use of myService&#39;, function() { myService.doStuff(); });</code></pre> <p>See also <a href="api/angular.mock.module"><code>angular.mock.module</code></a></p> <h4 id="description_example">Example</h4> <p>Example of what a typical jasmine tests looks like with the inject method. <pre class="prettyprint linenums"> angular.module('myApplicationModule', []) .value('mode', 'app') .value('version', 'v1.0.1'); describe('MyApp', function() { // You need to load modules that you want to test, // it loads only the "ng" module by default. beforeEach(module('myApplicationModule')); // inject() is used to inject arguments of all given functions it('should provide a version', inject(function(mode, version) { expect(version).toEqual('v1.0.1'); expect(mode).toEqual('app'); })); // The inject and module method can also be used inside of the it or beforeEach it('should override a version and test the new version is injected', function() { // module() takes functions or strings (module aliases) module(function($provide) { $provide.value('version', 'overridden'); // override version here }); inject(function(version) { expect(version).toEqual('overridden'); }); }); }); </pre> </div></div> <h2 id="usage">Usage</h2> <div class="usage"><pre class="prettyprint linenums">angular.mock.inject(fns);</pre> <h4 id="usage_parameters">Parameters</h4><table class="variables-matrix table table-bordered table-striped"><thead><tr><th>Param</th><th>Type</th><th>Details</th></tr></thead><tbody><tr><td>fns</td><td><a href="" class="label type-hint type-hint-object">...Function</a></td><td><div class="angular-mock-inject-page"><p>any number of functions which will be injected using the injector.</p> </div></td></tr></tbody></table></div> </div>