Bird
0
0

Given this test setup, what will component.getUserName() return?

medium📝 component behavior Q4 of 15
Angular - Testing
Given this test setup, what will component.getUserName() return?
const mockUserService = { getName: () => 'Alice' };
TestBed.configureTestingModule({
  declarations: [UserComponent],
  providers: [{ provide: UserService, useValue: mockUserService }]
});
const fixture = TestBed.createComponent(UserComponent);
const component = fixture.componentInstance;
A'Alice'
Bundefined
CThrows runtime error
D'Bob'
Step-by-Step Solution
Solution:
  1. Step 1: Analyze mock service method

    The mock service's getName method returns 'Alice'.
  2. Step 2: Understand component injection

    The component uses the mocked UserService, so calling getUserName() calls the mock's getName().
  3. Final Answer:

    'Alice' -> Option A
  4. Quick Check:

    Mock method return = 'Alice' [OK]
Quick Trick: Mock methods return defined values in tests [OK]
Common Mistakes:
  • Assuming real service is called
  • Expecting undefined without mock method
  • Confusing mock return with other values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes