Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { inject, TestBed } from '@angular/core/testing';
- import { BaseRequestOptions, Http, Response, ResponseOptions } from '@angular/http';
- import { MockBackend, MockConnection } from '@angular/http/testing';
- import { UserService } from './user.service';
- import { serialize } from '@angular/compiler/src/i18n/serializers/xml_helper';
- const mockResponse = [
- {
- 'id': 1,
- 'name': 'Leanne Graham',
- 'username': 'Bret',
- 'email': 'Sincere@april.biz',
- 'phone': '1-770-736-8031 x56442',
- 'website': 'hildegard.org',
- },
- {
- 'id': 2,
- 'name': 'Ervin Howell',
- 'username': 'Antonette',
- 'email': 'Shanna@melissa.tv',
- 'phone': '010-692-6593 x09125',
- 'website': 'anastasia.net',
- }
- ];
- describe('UserService', () => {
- beforeEach(() => {
- TestBed.configureTestingModule({
- // imports: [HttpModule],
- providers: [
- BaseRequestOptions,
- MockBackend,
- {
- provide: Http,
- useFactory: (backend, options) => new Http(backend, options),
- deps: [MockBackend, BaseRequestOptions],
- },
- UserService,
- ],
- });
- });
- let service;
- describe('getUsers', () => {
- beforeEach(inject([MockBackend, UserService, Http], (mockBackend: MockBackend, service: UserService, http: Http) => {
- service = new UserService(http);
- expect(service).toBeTruthy();
- mockBackend.connections.subscribe((connection: MockConnection) => {
- connection.mockRespond(new Response(new ResponseOptions({
- body: JSON.stringify(mockResponse),
- })));
- });
- }));
- it('should be covered', inject([UserService, Http],
- (userService: UserService, http: Http) => {
- service = new UserService(http);
- expect(service).toBeTruthy();
- }),
- );
- it('should get all users with IUser properties', inject(
- [UserService],
- (userService: UserService) => {
- userService.getAllUsers().subscribe(users => {
- users.forEach(user => {
- expect(user.email).toBeDefined();
- expect(user.name).toBeDefined();
- });
- });
- }),
- );
- });
- describe('getUser', () => {
- beforeEach(inject([MockBackend], (mockBackend: MockBackend) => {
- mockBackend.connections.subscribe((connection: MockConnection) => {
- connection.mockRespond(new Response(new ResponseOptions({
- body: JSON.stringify(mockResponse[0]),
- })));
- });
- }));
- it('should get an user with ICountry properties', inject(
- [UserService],
- (userService: UserService) => {
- userService.getUser(1).subscribe(user => {
- expect(user.email).toBeDefined();
- expect(user.name).toBeDefined();
- });
- }),
- );
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement