Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
How do I test an endpoint using Jasmine/Karma?

I have a method in one of my components that hits an endpoint like this...

private async getRolesAsync(): Promise<void> {
    const roles = await this.http.get<any>('https://jsonplaceholder.typicode.com/todos/1').toPromise();
    this.data = roles;
  }

Notice I have a fake endpoint in there, but what I'd like to know is how do I test that in a unit test? I want a unit test that tests to see if the endpoint works, i.e, it returns a 200 success.

I'm using Angular 13 as my application framework and Jasmine/Karma as my unit testing framework.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Please use this end-to-end running code for the resolution of all your issues. Coverage Logs

spec.ts file -

import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing";
import { TestBed, async, ComponentFixture } from "@angular/core/testing";
import { RouterTestingModule } from "@angular/router/testing";
import { AppComponent } from "./app.component";

describe("AppComponent", () => {
  let component: AppComponent;
  let fixture: ComponentFixture<AppComponent>;
  let httpTestingController: HttpTestingController;
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule,
        HttpClientTestingModule,
      ],
      declarations: [AppComponent],
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    httpTestingController = TestBed.inject(HttpTestingController)
    fixture.detectChanges();
  });

  it('#getRolesAsync should return expected data', async (done) => {
    const expectedData = {
      userId: 1,
      id: 1,
      title: "delectus aut autem",
      completed: false
    };
    (await component.getData()).subscribe(data => {
      expect(data).toEqual(expectedData);
      done();
    });
    const testRequest = httpTestingController.expectOne('https://jsonplaceholder.typicode.com/todos/1');
    testRequest.flush(expectedData);
  });
});

component.ts file -

import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"],
})
export class AppComponent {
  data: any;

  constructor(private http: HttpClient) { }

   async getData(): Promise<Observable<any>> {
    const roles = await this.http.get<any>('https://jsonplaceholder.typicode.com/todos/1');
    return this.data = roles;
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!