Bird
0
0

In an Angular test, if you forget to provide a mock service for a dependency injected via DI, what will happen when the component is created?

medium📝 Predict Output Q5 of 15
Angular - Services and Dependency Injection
In an Angular test, if you forget to provide a mock service for a dependency injected via DI, what will happen when the component is created?
AThe component will create the service instance itself
BThe component will use a default mock automatically
CThe test will fail with a 'No provider' error
DThe test will pass but with empty data
Step-by-Step Solution
Solution:
  1. Step 1: Recall Angular DI behavior without provider

    If no provider is registered for a dependency, Angular throws an error.
  2. Step 2: Match error to options

    The test will fail with a 'No provider' error correctly describes the 'No provider' error thrown during test setup.
  3. Final Answer:

    The test will fail with a 'No provider' error -> Option C
  4. Quick Check:

    Missing provider error = A [OK]
Quick Trick: Always provide mocks for injected services in tests [OK]
Common Mistakes:
MISTAKES
  • Assuming Angular auto-creates missing services
  • Expecting tests to pass silently
  • Thinking default mocks exist automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes