Brad Parks
Brad Parks

Reputation: 72261

Expected spy unknown to have been called

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.

Upvotes: 9

Views: 12064

Answers (3)

Abhishek Saxena
Abhishek Saxena

Reputation: 101

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);
       }));

Upvotes: 0

V. Nogueira
V. Nogueira

Reputation: 163

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']
    );

Upvotes: 0

Brad Parks
Brad Parks

Reputation: 72261

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.

Upvotes: 22

Related Questions