Skip to content

Instantly share code, notes, and snippets.

@Aridian1842
Created November 8, 2017 15:39
Show Gist options
  • Save Aridian1842/43ffce396c28b7f0b3639f1468cdb211 to your computer and use it in GitHub Desktop.
Save Aridian1842/43ffce396c28b7f0b3639f1468cdb211 to your computer and use it in GitHub Desktop.
Angular Unit Test Cheats
1) Cleanly providing stubs in a test
2) Unit testing an interceptor
3) Mocking injection token
/*************************************/
/**CLEANLY PROVIDING STUBS IN A TEST**/
/*************************************/
import {TestBed, inject, async} from '@angular/core/testing';
import { DependentService } from './dependent.service';
import {MasterService} from '../dataService/master.service';
describe('DependentService', () => {
let masterStub;
beforeEach(() => {
masterStub = jasmine.createSpyObj('master', ['action']);
TestBed.configureTestingModule({
providers: [
DependentService,
{provide: MasterService, useValue: masterStub}
]
});
});
it('checks dependent call', inject([DependentService, MasterService], (service: SearchHelperService, master: MasterService) => {
// call function which calls masterService's function
service.callMasterAction();
expect(master.action as jasmine.Spy).toHaveBeenCalled();
}));
/*******************************/
/**UNIT TESTING AN INTERCEPTOR**/
/*******************************/
import {TestBed, inject, async} from '@angular/core/testing';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {HTTP_INTERCEPTORS, HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {MyInterceptor} from './myInterceptor.interceptor';
describe('MyInterceptor', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: MyInterceptor,
multi: true
},
HttpClient
]
});
});
it('intercepts HTTP GET request',
async(inject([HttpClient, HttpTestingController],
(http: HttpClient, httpMock: HttpTestingController) => {
// random mock data
const url = '/mock';
const data = [{id: 5}];
// http get
http.get(url).subscribe((response) => {
// check side effects of your interceptor.
// For instance, that response is not changed
expect(response).toEqual(data);
});
const req = httpMock.expectOne((request) => request.url === url && request.method === 'GET');
req.flush(data);
// expect your interceptor's behaviour here. You could use spies and stubs for example
})));
/***************************************/
/**MOCKING INJECTION TOKEN : APP_TOKEN**/
/***************************************/
import {TestBed, inject, async} from '@angular/core/testing';
import {APP_TOKEN, AppToken} from '../../../core/config/appToken';
describe('Injection Token Mock', () => {
let appTokenStub;
beforeEach(() => {
appConfigStub = {
endpoint: 'http://example.com'
};
TestBed.configureTestingModule({
imports: [
HttpModule
],
providers: [
Http,
{provide: APP_TOKEN, useValue: appTokenStub}
]
});
});
it('checks value of app_token', inject([APP_TOKEN], (token: AppToken) => {
expect(token).toEqual(appConfigStub);
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment