Advertisement
fabiobiondi

Angular Unit Test with MockBackend

Jun 19th, 2017
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. import { inject, TestBed } from '@angular/core/testing';
  2. import { BaseRequestOptions, Http, Response, ResponseOptions } from '@angular/http';
  3. import { MockBackend, MockConnection } from '@angular/http/testing';
  4. import { UserService } from './user.service';
  5. import { serialize } from '@angular/compiler/src/i18n/serializers/xml_helper';
  6.  
  7. const mockResponse = [
  8. {
  9. 'id': 1,
  10. 'name': 'Leanne Graham',
  11. 'username': 'Bret',
  12. 'email': 'Sincere@april.biz',
  13. 'phone': '1-770-736-8031 x56442',
  14. 'website': 'hildegard.org',
  15. },
  16. {
  17. 'id': 2,
  18. 'name': 'Ervin Howell',
  19. 'username': 'Antonette',
  20. 'email': 'Shanna@melissa.tv',
  21. 'phone': '010-692-6593 x09125',
  22. 'website': 'anastasia.net',
  23. }
  24. ];
  25. describe('UserService', () => {
  26.  
  27. beforeEach(() => {
  28. TestBed.configureTestingModule({
  29. // imports: [HttpModule],
  30. providers: [
  31. BaseRequestOptions,
  32. MockBackend,
  33. {
  34. provide: Http,
  35. useFactory: (backend, options) => new Http(backend, options),
  36. deps: [MockBackend, BaseRequestOptions],
  37. },
  38. UserService,
  39. ],
  40. });
  41. });
  42.  
  43. let service;
  44.  
  45. describe('getUsers', () => {
  46.  
  47. beforeEach(inject([MockBackend, UserService, Http], (mockBackend: MockBackend, service: UserService, http: Http) => {
  48. service = new UserService(http);
  49. expect(service).toBeTruthy();
  50.  
  51. mockBackend.connections.subscribe((connection: MockConnection) => {
  52. connection.mockRespond(new Response(new ResponseOptions({
  53. body: JSON.stringify(mockResponse),
  54. })));
  55. });
  56. }));
  57.  
  58. it('should be covered', inject([UserService, Http],
  59. (userService: UserService, http: Http) => {
  60. service = new UserService(http);
  61. expect(service).toBeTruthy();
  62. }),
  63. );
  64.  
  65. it('should get all users with IUser properties', inject(
  66. [UserService],
  67. (userService: UserService) => {
  68. userService.getAllUsers().subscribe(users => {
  69. users.forEach(user => {
  70. expect(user.email).toBeDefined();
  71. expect(user.name).toBeDefined();
  72. });
  73. });
  74. }),
  75. );
  76. });
  77.  
  78. describe('getUser', () => {
  79.  
  80. beforeEach(inject([MockBackend], (mockBackend: MockBackend) => {
  81. mockBackend.connections.subscribe((connection: MockConnection) => {
  82. connection.mockRespond(new Response(new ResponseOptions({
  83. body: JSON.stringify(mockResponse[0]),
  84. })));
  85. });
  86. }));
  87.  
  88. it('should get an user with ICountry properties', inject(
  89. [UserService],
  90. (userService: UserService) => {
  91. userService.getUser(1).subscribe(user => {
  92. expect(user.email).toBeDefined();
  93. expect(user.name).toBeDefined();
  94. });
  95. }),
  96. );
  97. });
  98.  
  99. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement