60 lines
2 KiB
HTML
Executable file
60 lines
2 KiB
HTML
Executable file
<a href="http://github.com/angular/angular.js/edit/master/docs/content/guide/dev_guide.services.testing_services.ngdoc" class="improve-docs btn btn-primary"><i class="icon-edit"> </i> Improve this doc</a><h1><code ng:non-bindable=""></code>
|
|
<div><span class="hint"></span>
|
|
</div>
|
|
</h1>
|
|
<div><div class="developer-guide-page developer-guide-angular-services-testing-angular-services-page"><p>The following is a unit test for the 'notify' service in the 'Dependencies' example in <a href="guide/dev_guide.services.creating_services">Creating Angular Services</a>. The unit test example uses Jasmine
|
|
spy (mock) instead of a real browser alert.</p>
|
|
<pre class="prettyprint linenums">
|
|
var mock, notify;
|
|
|
|
beforeEach(function() {
|
|
mock = {alert: jasmine.createSpy()};
|
|
|
|
module(function($provide) {
|
|
$provide.value('$window', mock);
|
|
});
|
|
|
|
inject(function($injector) {
|
|
notify = $injector.get('notify');
|
|
});
|
|
});
|
|
|
|
it('should not alert first two notifications', function() {
|
|
notify('one');
|
|
notify('two');
|
|
|
|
expect(mock.alert).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should alert all after third notification', function() {
|
|
notify('one');
|
|
notify('two');
|
|
notify('three');
|
|
|
|
expect(mock.alert).toHaveBeenCalledWith("one\ntwo\nthree");
|
|
});
|
|
|
|
it('should clear messages after alert', function() {
|
|
notify('one');
|
|
notify('two');
|
|
notify('third');
|
|
notify('more');
|
|
notify('two');
|
|
notify('third');
|
|
|
|
expect(mock.alert.callCount).toEqual(2);
|
|
expect(mock.alert.mostRecentCall.args).toEqual(["more\ntwo\nthird"]);
|
|
});
|
|
</pre>
|
|
<h3>Related Topics</h2>
|
|
<ul>
|
|
<li><a href="guide/dev_guide.services.understanding_services">Understanding Angular Services</a></li>
|
|
<li><a href="guide/dev_guide.services.creating_services">Creating Angular Services</a></li>
|
|
<li><a href="guide/dev_guide.services.managing_dependencies">Managing Service Dependencies</a></li>
|
|
<li><a href="guide/dev_guide.services.injecting_controllers">Injecting Services Into Controllers</a></li>
|
|
</ul>
|
|
<h2>Related API</h3>
|
|
<ul>
|
|
<li><a href="api/ng">Angular Service API</a></li>
|
|
</ul>
|
|
</div></div>
|