Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Commit a2820bb

Browse files
committed
feat(service): add service recipe
1 parent 469b3e1 commit a2820bb

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

jquery-events/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

service/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Service
2+
> Testing recipes for Services
3+
4+
## Table of contents
5+
6+
- [Boilerplate](#boilerplate)
7+
- [Exposed properties](#expose-properties)
8+
- [Exposed methods](#expose-methods)
9+
- [Methods from other services](#methods-from-other-services)
10+
- [$http]($http)
11+
- [Promises](#promises)
12+
13+
## Boilerplate
14+
15+
Before start we need to initialize the service and mock it's dependencies in our tests:
16+
17+
```js
18+
describe('SampleService', function() {
19+
'use strict';
20+
21+
var sampleService;
22+
23+
beforeEach(module('myApp'));
24+
25+
beforeEach(inject(function(_sampleService_) {
26+
sampleService = _sampleService_;
27+
}));
28+
29+
...
30+
});
31+
```
32+
33+

service/service.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('myApp')
5+
.factory('sampleService', SampleService);
6+
7+
function SampleService() {
8+
var service = {
9+
foo: 'bar',
10+
bar: bar
11+
};
12+
13+
return service;
14+
15+
function bar() {
16+
service.foo = 'baz';
17+
}
18+
}
19+
20+
}());

service/service.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
describe('SampleService', function() {
2+
'use strict';
3+
4+
var sampleService;
5+
6+
beforeEach(module('myApp'));
7+
8+
beforeEach(inject(function(_sampleService_) {
9+
sampleService = _sampleService_;
10+
}));
11+
12+
it('should expose a property', function() {
13+
expect(sampleService.foo).toBe('bar');
14+
});
15+
16+
it('should expose a method', function() {
17+
sampleService.bar();
18+
expect(sampleService.foo).toBe('baz');
19+
});
20+
});

services/README.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)