Expected spy unknown to have been called
Asked Answered
H

3

8

I'm new to Jasmine, and am getting this error:

Expected spy unknown to have been called.

What does this mean? I'm spying on a method, but am not sure what unknown means.

Hid answered 4/5, 2018 at 13:25 Comment(0)
H
22

The answer is really simple! The spy didnt have a name, so it's called "unknown" by default.

To name it, I simply did this

var mySpy = jasmine.createSpy("JamesBond");

Then it failed with something more readable!

Expected spy JamesBond to have been called.
Hid answered 4/5, 2018 at 13:25 Comment(0)
H
0

The "unknown" value is due to the inexistence of the name attribution for the spy.

before: Error: Expected spy unknown.getPlans to have been called.

const mockPlansServiceEmpty: Spide<PlansService> = jasmine.createSpyObj(
      ['getPlans', 'separateValidFromInvalidPlans']
    );

after: Error: Expected spy mockPlansEmpty.getPlans to have been called.

const mockPlansServiceEmpty: Spide<PlansService> = jasmine.createSpyObj(
      'mockPlansEmpty',
      ['getPlans', 'separateValidFromInvalidPlans']
    );
Hodgepodge answered 10/12, 2021 at 2:20 Comment(0)
E
0

If you are using the service with promise then you can use async await in the test case.

 it ('should check the testAngular with error return', **fakeAsync**( () => {
        // Arrange
        let promise = Promise.reject(Error);
        testAngular.error = jasmine.createSpy();
        testAngular.yourfunctioname= jasmine.createSpy().and.returnValue(promise);
    
        //Act
        **await** fixture.detectChanges();
    
        // Assert
**flush()**
        expect(alertServiceSpy.error).toHaveBeenCalledWith(Error);
       }));
Exarchate answered 18/5, 2023 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.