0
0
Angularframework~20 mins

Mocking services in tests in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will the component display when the mocked service returns a specific value?

Given an Angular component that uses a mocked service returning of('Hello Mock'), what will the component render?

Angular
import { Component } from '@angular/core';
import { of } from 'rxjs';

class MockService {
  getData() {
    return of('Hello Mock');
  }
}

@Component({
  selector: 'app-test',
  template: `<p>{{ message }}</p>`
})
export class TestComponent {
  message = '';
  constructor(private service: MockService) {
    this.service.getData().subscribe(data => this.message = data);
  }
}
A<p>Error</p>
B<p>undefined</p>
C<p></p>
D<p>Hello Mock</p>
Attempts:
2 left
💡 Hint

Think about what the mocked service returns and how the component subscribes to it.

📝 Syntax
intermediate
2:00remaining
Which option correctly mocks a service method returning a Promise in Angular test?

In an Angular test, you want to mock a service method fetchData() that returns a Promise resolving to 42. Which mock implementation is correct?

AfetchData: () => Promise.resolve(42)
BfetchData: () => new Promise(42)
CfetchData: () => Promise(42)
DfetchData: () => Promise.reject(42)
Attempts:
2 left
💡 Hint

Remember how to create a resolved Promise with a value.

🔧 Debug
advanced
2:00remaining
Why does the test fail when the mocked service method is not called?

In an Angular test, the mocked service method getData() is expected to be called once, but the test fails saying it was never called. What is the most likely cause?

AThe mocked service method is called twice instead of once.
BThe component does not inject the mocked service but the real one.
CThe test does not subscribe to the observable returned by the mocked method.
DThe mocked service method returns the wrong data type.
Attempts:
2 left
💡 Hint

Check which service instance the component uses during the test.

lifecycle
advanced
2:00remaining
When is the mocked service method called in the component lifecycle?

In an Angular component, a mocked service method is called inside ngOnInit(). When will this method be invoked during testing?

AImmediately after component creation and before change detection.
BOnly after the first change detection cycle completes.
CAfter the component is destroyed.
DOnly when a user triggers an event.
Attempts:
2 left
💡 Hint

Recall when ngOnInit() runs in Angular.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using a mocked service in Angular tests?

Why do Angular developers use mocked services instead of real services in unit tests?

ATo improve the performance of the real service during tests.
BTo automatically generate UI snapshots for the component.
CTo isolate the component and test it without dependencies affecting the result.
DTo ensure the real backend API is always called during tests.
Attempts:
2 left
💡 Hint

Think about what unit tests aim to achieve regarding dependencies.