SIGN IN SIGN UP
angular / angular.js UNCLAIMED

AngularJS - HTML enhanced for web apps!

0 0 0 JavaScript
2011-06-06 08:50:35 -07:00
@ngdoc overview
@name Developer Guide: Angular Services: Testing Angular Services
@description
2012-08-09 00:12:54 +10: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 08:50:35 -07:00
<pre>
var mock, notify;
beforeEach(function() {
mock = {alert: jasmine.createSpy()};
2012-01-15 23:28:10 -08:00
module(function($provide) {
$provide.value('$window', mock);
});
inject(function($injector) {
notify = $injector.get('notify');
});
2011-06-06 08:50:35 -07:00
});
it('should not alert first two notifications', function() {
notify('one');
notify('two');
expect(mock.alert).not.toHaveBeenCalled();
2011-06-06 08:50:35 -07: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 08:50:35 -07: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 08:50:35 -07: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 08:50:35 -07:00
## Related API
* {@link api/ng Angular Service API}