mercredi 30 janvier 2019

Testing subscribe in service

Help please test the service. I found a lot of information about testing, but not one did not help.

admin.service.ts

  private bcUrl = 'http://30.30.0.101:8080';
  private httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': this.cookieService.get('token')
    })
  };

  constructor(
    private http: HttpClient,
    private cookieService: CookieService,
    private userService: UserService
  ) {
  }

  getUsers(page, lim): any {
    return this.http.get(`${this.bcUrl}/api/security/users?page=${page}&size=${lim}`, this.httpOptions)
      .subscribe(
        (data: DataInterface) => {
          this.users = data.content;
          this.st.totalElements = data.totalElements;
          this.st.totalPages = data.totalPages;
          this.dataSource = data.content;
        },
        error => {
          if (error.status === 404) {
            alert('Сторінка не знайдена');
          }
          if (error.status === 401) {
            alert('Немає прав доступу');
          }
        });
  }

This is part of the code. I would be very happy if you help test it. Or send to the desired resource.

Here is my test. I started writing it, but I don’t know where to go any further.

import {async, getTestBed, inject, TestBed} from '@angular/core/testing';
import {CookieService} from 'ngx-cookie-service';
import {AdminService} from './admin.service';
import {HttpClientTestingModule} from '@angular/common/http/testing';
import {UserService} from '@app/core/user/user.service';
import {RequestMethod, ResponseOptions} from '@angular/http';

describe('AdminService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [AdminService, CookieService, UserService]
    });
  });
  it('should be created', inject([AdminService], (service: AdminService) => {
    expect(service).toBeTruthy();
  }));


  it('should be getUsers', async(() => {
    const adminService: AdminService = getTestBed().get(AdminService);
mockBackend.connections.subscribe(connection => {
  expect(connection.request.method).toBe(RequestMethod.Delete);
  connection.mockRespond( new Response(new ResponseOptions()))
});

  }));


});

Aucun commentaire:

Enregistrer un commentaire