Skip to content
This repository has been archived by the owner on May 8, 2020. It is now read-only.

Commit

Permalink
feat(documentation): Added doc pages for unit testing, fixed missing …
Browse files Browse the repository at this point in the history
…LoggerMock export
  • Loading branch information
zakhenry committed Jul 6, 2016
1 parent 203cb51 commit a2d889c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
78 changes: 76 additions & 2 deletions docs/guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,81 @@ date: 2016-06-09
collection: guide
collectionSort: 1
layout: guide.hbs
pendingTask: true
---

Testing infrastructure has been implemented, just need to document how to use it
## Unit Testing
### Overview

Unit testing in Ubiquits follows the exact same pattern as Angular 2. As such, it uses the [Jasmine] BDD framework, with
extra methods for dependency injection and handling of providers.

### Example with dependency injection

```typescript
import { Logger, LoggerMock } from '@ubiquits/core/common';
import { addProviders, inject, async } from '@angular/core/testing';
import { Injectable } from '@angular/core';
import Spy = jasmine.Spy;

@Injectable()
class ExampleService {

constructor(protected logger: Logger) {}

public testLog(message: string): this {
this.logger.debug(message);
return this;
}

public testLogAsync(message: string): Promise<this> {
return Promise.resolve()
.then(() => {
this.testLog(message);
return this;
});
}

}

const providers = [
{provide: Logger, useClass: LoggerMock},
ExampleService
];

describe('Example service', () => {

beforeEach(() => {
addProviders(providers);
});

it('logs messages passed to it',
inject([ExampleService, Logger],
(service: ExampleService, logger: Logger) => {

const loggerSpy: Spy = spyOn(logger, 'persistLog');

service.testLog('hello world');

expect(loggerSpy).toHaveBeenCalledWith('debug', ['hello world']);
}));

it('logs messages passed to it after promise is resolved',
async(inject([ExampleService, Logger],
(service: ExampleService, logger: Logger) => {

const loggerSpy: Spy = spyOn(logger, 'persistLog');

expect(loggerSpy).not.toHaveBeenCalledWith('debug', ['hello world async']);

return service.testLogAsync('hello world async')
.then(() => {
expect(loggerSpy).toHaveBeenCalledWith('debug', ['hello world async']);
});

})));

});
```


[jasmine]: http://jasmine.github.io/2.4/introduction.html
1 change: 1 addition & 0 deletions src/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/** End Typedoc Module Declaration */
export * from './consoleLogger.service';
export * from './logger.service';
export * from './logger.service.mock';
export * from './service';

0 comments on commit a2d889c

Please sign in to comment.