0
0
Angularframework~20 mins

TestBed configuration in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TestBed Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Angular test setup?
Given the TestBed configuration and component below, what will be the text content of the

element after fixture.detectChanges()?

Angular
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';

@Component({
  selector: 'app-greet',
  template: `<p>{{ greeting }}</p>`
})
class GreetComponent {
  greeting = 'Hello, Angular!';
}

beforeEach(() => {
  TestBed.configureTestingModule({
    declarations: [GreetComponent]
  });
});

const fixture = TestBed.createComponent(GreetComponent);
fixture.detectChanges();
const pElement = fixture.nativeElement.querySelector('p');
const text = pElement.textContent;
A"Hello, Angular!"
B"{{ greeting }}"
C"Hello, World!"
D""
Attempts:
2 left
💡 Hint
Remember that fixture.detectChanges() triggers Angular's change detection to update the template bindings.
📝 Syntax
intermediate
2:00remaining
Which TestBed configuration will cause a runtime error when creating the component?
Consider the following TestBed configurations. Which one will cause an error when calling TestBed.createComponent(MyComponent)?
Angular
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';

@Component({
  selector: 'app-my',
  template: `<div>Test</div>`
})
class MyComponent {}

// TestBed configurations:
// A: declarations: [MyComponent]
// B: imports: [MyComponent]
// C: providers: [MyComponent]
// D: declarations: []
ATestBed.configureTestingModule({ declarations: [] });
BTestBed.configureTestingModule({ imports: [MyComponent] });
CTestBed.configureTestingModule({ providers: [MyComponent] });
DTestBed.configureTestingModule({ declarations: [MyComponent] });
Attempts:
2 left
💡 Hint
Components must be declared in the declarations array to be created.
🔧 Debug
advanced
2:00remaining
Why does this TestBed test fail to inject the service?
Given this TestBed setup, why does the test fail with 'No provider for DataService' error?
Angular
import { Injectable } from '@angular/core';
import { TestBed } from '@angular/core/testing';

@Injectable()
class DataService {
  getData() { return 'data'; }
}

beforeEach(() => {
  TestBed.configureTestingModule({
    declarations: [],
    providers: []
  });
});

const service = TestBed.inject(DataService);
ADataService must be imported from '@angular/common' to be injectable.
BDataService must be declared in declarations, not providers.
CDataService is not added to the providers array in TestBed configuration.
DTestBed.inject() can only inject components, not services.
Attempts:
2 left
💡 Hint
Services must be listed in providers to be injectable in tests.
state_output
advanced
2:00remaining
What is the value of 'count' after this TestBed test runs?
In this test, what will be the value of component.count after fixture.detectChanges()?
Angular
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';

@Component({
  selector: 'app-counter',
  template: `<button (click)="increment()">Increment</button><p>{{ count }}</p>`
})
class CounterComponent {
  count = 0;
  increment() { this.count++; }
}

beforeEach(() => {
  TestBed.configureTestingModule({ declarations: [CounterComponent] });
});

const fixture = TestBed.createComponent(CounterComponent);
const component = fixture.componentInstance;
component.increment();
fixture.detectChanges();
A0
B1
Cundefined
DNaN
Attempts:
2 left
💡 Hint
Calling the increment method increases count before change detection.
🧠 Conceptual
expert
3:00remaining
Which TestBed configuration correctly mocks a service dependency?
You have a component that depends on ApiService. You want to test the component with a mock service that returns fixed data. Which TestBed configuration correctly provides the mock?
Angular
class ApiService {
  fetch() { return 'real data'; }
}

class MockApiService {
  fetch() { return 'mock data'; }
}

TestBed.configureTestingModule({
  declarations: [MyComponent],
  // Which providers array is correct?
  providers: ???
});
Aproviders: [{ provide: MockApiService, useClass: ApiService }]
Bproviders: [{ provide: ApiService, useValue: ApiService }]
Cproviders: [ApiService, MockApiService]
Dproviders: [{ provide: ApiService, useClass: MockApiService }]
Attempts:
2 left
💡 Hint
Use the provide/useClass pattern to replace a service with a mock in tests.