angular.js/docs/content/guide/dev_guide.services.testing_services.ngdoc

63 lines
1.6 KiB
Text
Raw Permalink Normal View History

2011-06-06 15:50:35 +00:00
@ngdoc overview
@name Developer Guide: Angular Services: Testing Angular Services
@description
2012-08-08 14:12:54 +00:00
The following is a unit test for the 'notify' service in the 'Dependencies' example in {@link
dev_guide.services.creating_services Creating Angular Services}. The unit test example uses Jasmine
spy (mock) instead of a real browser alert.
2011-06-06 15:50:35 +00:00
<pre>
var mock, notify;
beforeEach(function() {
mock = {alert: jasmine.createSpy()};
2012-01-16 07:28:10 +00:00
module(function($provide) {
$provide.value('$window', mock);
});
inject(function($injector) {
notify = $injector.get('notify');
});
2011-06-06 15:50:35 +00:00
});
it('should not alert first two notifications', function() {
notify('one');
notify('two');
expect(mock.alert).not.toHaveBeenCalled();
2011-06-06 15:50:35 +00:00
});
it('should alert all after third notification', function() {
notify('one');
notify('two');
notify('three');
expect(mock.alert).toHaveBeenCalledWith("one\ntwo\nthree");
2011-06-06 15:50:35 +00:00
});
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"]);
2011-06-06 15:50:35 +00:00
});
</pre>
## Related Topics
* {@link dev_guide.services.understanding_services Understanding Angular Services}
* {@link dev_guide.services.creating_services Creating Angular Services}
* {@link dev_guide.services.managing_dependencies Managing Service Dependencies}
* {@link dev_guide.services.injecting_controllers Injecting Services Into Controllers}
2011-06-06 15:50:35 +00:00
## Related API
* {@link api/ng Angular Service API}