Testing Questions with Answers

8 practice questions on Testing (Angular) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

What does TestBed.createComponent(MyComponent) return?

  1. A ComponentFixture wrapping the component and its DOM
  2. A new injector that has no providers at all
  3. A Promise that resolves once the component's template has finished compiling
  4. The bare component instance, without any DOM

Answer: A. A ComponentFixture wrapping the component and its DOM

Explanation

createComponent creates the component inside the test module and returns a ComponentFixture. The fixture gives access to the instance (componentInstance), the DOM (nativeElement) and change detection (detectChanges).

Step-by-step solution

  1. TestBed.createComponent builds the component using the test module's providers.
  2. It returns a ComponentFixture rather than just the instance.
  3. The fixture lets the test read the component, its DOM and run change detection.

Why the other options are wrong

  • B. TestBed creates the module and providers; createComponent does not return an injector.
  • C. compileComponents is the call that returns a Promise, not createComponent.
  • D. The fixture is returned; the instance is reached through its componentInstance property.

Tip: Fixture = instance + DOM + change detection in one object.

Question 2Easy

Which Jasmine function groups related tests into one suite?

  1. it
  2. expect
  3. describe
  4. spyOn

Answer: C. describe

Explanation

describe(name, fn) creates a suite that groups related specs. it defines one individual test inside a suite, expect checks a value, and spyOn replaces a method with a spy.

Step-by-step solution

  1. A suite groups specs about one subject.
  2. describe creates that group; it creates one spec inside it.
  3. So the grouping function is describe.

Why the other options are wrong

  • A. it defines a single test, not a group of tests.
  • B. expect makes an assertion about a value.
  • D. spyOn replaces a method with a spy so calls can be tracked.

Tip: describe for the group, it for each test.

Question 3Easy

What does jasmine.createSpyObj('Api', ['getUser']) give you?

  1. An object with a spy method getUser you can stub and inspect
  2. A real Api instance that makes actual network calls when its methods run
  3. A test suite that runs the getUser method two times
  4. A mock that throws on every call until it is reset

Answer: A. An object with a spy method getUser you can stub and inspect

Explanation

createSpyObj builds a plain object whose listed methods are spies. The test can set return values on them and check how they were called, which is how a service is replaced in a unit test.

Step-by-step solution

  1. createSpyObj takes a name and an array of method names.
  2. It returns an object where each listed method is a spy.
  3. The test stubs those methods and then checks the calls.

Why the other options are wrong

  • B. A spy object has no real implementation unless you add one, so no network calls happen.
  • C. createSpyObj does not run any tests; it builds a test double.
  • D. Spies do not throw by default; they return undefined unless configured.

Tip: Use a spy object as a fake service in a component test.

Question 4Medium

Which tool configures a test module for components?

  1. TestBed
  2. Jasmine
  3. ComponentRunner
  4. Karma only

Answer: A. TestBed

Explanation

TestBed.configureTestingModule sets up the test environment.

Step-by-step solution

  1. Testing utility.

Why the other options are wrong

  • B. Jasmine is the test framework; TestBed configures the module.
  • C. ComponentRunner is not an Angular testing utility.
  • D. Karma is a test runner, not the tool that configures test modules.

Question 5Medium

After changing a property in a component test, what is needed to update the DOM?

  1. fixture.refresh() to redraw
  2. fixture.render() to render
  3. fixture.detectChanges()
  4. fixture.update() to patch

Answer: C. fixture.detectChanges()

Explanation

Tests run change detection manually.

Step-by-step solution

  1. Manual CD.

Why the other options are wrong

  • A. fixture has no refresh method; detectChanges triggers rendering.
  • B. fixture has no render method for updating the DOM.
  • D. fixture has no update method; detectChanges is the call used.

Question 6Medium

Inside fakeAsync, what does flush() do?

  1. It clears all spies created in this test
  2. It waits in real time for every timer to finish
  3. It runs only pending microtasks, such as resolved promises
  4. It runs pending timers now, without waiting

Answer: D. It runs pending timers now, without waiting

Explanation

flush() runs pending macrotasks such as setTimeout callbacks right away, inside the fake clock. The test does not have to wait in real time. tick(ms) advances the clock by a set amount, while flush() runs what is already queued.

Step-by-step solution

  1. fakeAsync replaces the real timers with a fake clock.
  2. flush() runs the timers that are already queued, skipping the wait.
  3. The test then checks the results straight away.

Why the other options are wrong

  • A. Spies are not touched by flush(); it runs queued work.
  • B. flush() is the fake-clock way to skip the wait, not a real-time wait.
  • C. Microtasks are a separate call: flushMicrotasks().

Tip: tick(ms) moves time forward; flush() runs everything queued now.

Question 7Hard

What does fakeAsync with tick() let you do?

  1. Skip tests
  2. Run tests in a real browser tab only, never in a headless browser
  3. Make HTTP real
  4. Simulate the passage of time for timers synchronously

Answer: D. Simulate the passage of time for timers synchronously

Explanation

Timers advance without waiting.

Step-by-step solution

  1. Control time.

Why the other options are wrong

  • A. fakeAsync with tick runs tests; it does not skip them.
  • B. fakeAsync works in any environment, including headless browsers.
  • C. fakeAsync simulates time; it does not make real HTTP calls.

Question 8Hard

In a test using HttpClientTestingModule, what does httpMock.verify() in afterEach check?

  1. That the service instance was created by TestBed with providers
  2. That no HTTP request is still open or unmatched
  3. That the response body matches a saved snapshot file
  4. That every response status code was 200 in the test

Answer: B. That no HTTP request is still open or unmatched

Explanation

HttpTestingController.verify() fails the test if any request was made and not matched by expectOne or match, or if any request is still open. It catches calls the test did not expect.

Step-by-step solution

  1. The test uses expectOne or match to answer each HTTP request it expects.
  2. verify() looks for requests that were made but never answered in the test.
  3. If one is found, the test fails, so stray HTTP calls are caught.

Why the other options are wrong

  • A. Creating the service is a TestBed job; verify() checks HTTP traffic.
  • C. verify() does not compare responses with snapshots.
  • D. verify() does not look at status codes; it checks that requests were matched.

Tip: Pair HttpClientTestingModule with verify() in afterEach to catch stray requests.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Angular topics · All topics