0
0
Angularframework~20 mins

Service testing with dependency injection in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Service Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Angular service test?

Given this Angular service and its test, what will the test output?

Angular
import { TestBed } from '@angular/core/testing';
import { DataService } from './data.service';

class MockDataService {
  getData() {
    return 'mock data';
  }
}

describe('DataService with DI', () => {
  let service: DataService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [{ provide: DataService, useClass: MockDataService }]
    });
    service = TestBed.inject(DataService);
  });

  it('should return mock data', () => {
    expect(service.getData()).toBe('mock data');
  });
});
AThe test passes but returns undefined because getData is not mocked.
BThe test fails because the real DataService is used instead of the mock.
CThe test passes because the mock service returns 'mock data'.
DThe test throws an error because DataService is not provided.
Attempts:
2 left
💡 Hint

Think about how dependency injection replaces the real service with the mock.

🔧 Debug
intermediate
2:00remaining
Why does this Angular test fail with 'No provider for HttpClient' error?

Consider this test setup for a service that uses HttpClient. Why does it fail?

Angular
import { TestBed } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { ApiService } from './api.service';

describe('ApiService', () => {
  let service: ApiService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientModule],
      providers: [ApiService]
    });
    service = TestBed.inject(ApiService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });
});
AHttpClientModule is not imported in the test module, so HttpClient provider is missing.
BApiService is not provided in the test module, causing the error.
CHttpClient is mocked incorrectly causing the error.
DTestBed.inject is used incorrectly; it should be TestBed.get.
Attempts:
2 left
💡 Hint

HttpClient needs a module to provide it in Angular tests.

📝 Syntax
advanced
2:00remaining
Which option correctly injects a mocked service in Angular test?

Choose the correct way to inject a mocked LoggerService in an Angular test.

Angular
class MockLoggerService {
  log(message: string) { return 'mock log: ' + message; }
}

beforeEach(() => {
  TestBed.configureTestingModule({
    // What goes here?
  });
  const logger = TestBed.inject(LoggerService);
});
Aproviders: [{ provide: LoggerService, useClass: MockLoggerService }]
Bproviders: [LoggerService, MockLoggerService]
Cproviders: [{ provide: MockLoggerService, useClass: LoggerService }]
Dproviders: [{ useClass: MockLoggerService }]
Attempts:
2 left
💡 Hint

Remember to provide the original service token and specify the mock class.

state_output
advanced
2:00remaining
What is the value of 'result' after this Angular service test runs?

Given this test with dependency injection, what is the value of result after service.getValue() is called?

Angular
class RealService {
  getValue() { return 42; }
}

class FakeService {
  getValue() { return 100; }
}

let result: number;

beforeEach(() => {
  TestBed.configureTestingModule({
    providers: [{ provide: RealService, useClass: FakeService }]
  });
  const service = TestBed.inject(RealService);
  result = service.getValue();
});
Aundefined
B42
CThrows an error because RealService is not provided
D100
Attempts:
2 left
💡 Hint

Check which class is actually injected for RealService.

🧠 Conceptual
expert
2:00remaining
Which statement best describes Angular's dependency injection in service testing?

Choose the most accurate description of how Angular's dependency injection works in service testing.

AAngular always uses the real service implementation regardless of test configuration.
BAngular replaces the requested service with the provided mock or alternative class during test injection.
CAngular requires manual instantiation of services in tests without using TestBed.
DAngular's dependency injection only works for components, not services.
Attempts:
2 left
💡 Hint

Think about how TestBed providers affect injected services.