Podcast/lib/angular/docs/partials/guide/dev_guide.services.testing_services.html
2013-04-07 10:12:25 +02:00

63 lines
1.7 KiB
HTML

<h1><code ng:non-bindable=""></code>
<span class="hint"></span>
</h1>
<div><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</h3>
<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>
<h3>Related API</h3>
<ul>
<li><a href="api/ng">Angular Service API</a></li>
</ul></div>