Skip to content

Instantly share code, notes, and snippets.

@night-fury-rider
Last active May 31, 2020 03:40
Show Gist options
  • Save night-fury-rider/8761ec59a92c7be28870565318105214 to your computer and use it in GitHub Desktop.
Save night-fury-rider/8761ec59a92c7be28870565318105214 to your computer and use it in GitHub Desktop.
Unit testing Angular Application using Jasmine - globalmocks
// Use shollow copy of your app data.
export const appData = {
defaultOrderIndex: 0,
sortOrders: [{
type: 'string',
name: 'Title',
sortAttribute: 'title'
}]
// Remaining test data should be added here.
}
import { Component, Input } from '@angular/core';
// Write a stub component as needded.
@Component({selector: 'app-home', template: ''})
export class HomeStubComponent{ @Input() appData; } // here appData is @Input for HomeComponent
// Create a spy object on HomeService.
export const HomeService = jasmine.createSpyObj('HomeService', ['get', 'set', 'searchSubscriber$']);
// Mock for method of HomeService.
HomeService.get.and.callFake(() => {
return {
name: 'Yuvraj'
};
});
// Callback mock for method of HomeService.
HomeService.set.and.callFake((callback) => {
callback();
});
// Nested callback mock for method of HomeService.
HomeService.searchSubscriber$ = {
subscribe: (callback) => {
callback('Yuvraj Patil');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment