Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a mock service using Jasmine spy.
Angular
const mockService = jasmine.[1]('MyService');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using spyOn instead of createSpyObj to create the mock object.
Trying to create a spy without specifying method names.
✗ Incorrect
Use createSpyObj to create a mock object with spy methods.
2fill in blank
mediumComplete the code to provide the mock service in the Angular test module.
Angular
TestBed.configureTestingModule({ providers: [{ provide: MyService, use[1]: mockService }] }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using useClass instead of useValue for a mock object.
Forgetting to provide the mock service in the test module.
✗ Incorrect
Use useValue to provide a mock object instance as the service.
3fill in blank
hardFix the error in spying on the service method in the test.
Angular
spyOn(mockService, '[1]').and.returnValue(of(true));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect casing for the method name.
Using underscores or other naming styles not matching the service.
✗ Incorrect
Method names are case-sensitive and usually camelCase in Angular services.
4fill in blank
hardFill both blanks to create a mock service with two spy methods.
Angular
const mockService = jasmine.createSpyObj('MyService', [[1], [2]]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around method names.
Providing method names not matching the service interface.
✗ Incorrect
Provide method names as strings in an array to create spies for each.
5fill in blank
hardFill all three blanks to spy on a method and set a return value in the test.
Angular
spyOn(mockService, [1]).and.[2](of([3]));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'return' instead of 'returnValue'.
Not quoting the method name.
Passing a non-Observable value without wrapping in 'of()'.
✗ Incorrect
Use spyOn with the method name as a string, then and.returnValue to set the mock return value.