Cannot get spyOn test to work properly- Angular

Cannot get spyOn test to work properly- Angular



I cannot get my test to work for some reason and it keeps throwing the error:



Expected spy on isCurrentStatus to equal true.



The function being called simply assessed whether the passed in variable equals the currently held status property, and returns either true of false. Nothing major...


status



Test


it('should return true if current status = status passed in', () =>
const statusSpy = spyOn(component, 'isCurrentStatus');
component.event = failedEvent;
component.isCurrentStatus('failed');
expect(statusSpy).toEqual(true);
)



component


event: MyEvent;

isCurrentStatus(status: string): boolean
return this.event.status === status;



UPDATE



I just moved the spyOn into the beforeEach() section and now returns:


spyOn


beforeEach()



expected undefined to equal true


undefined


true




3 Answers
3



you could create a spyOn on the function and check value it returns differently:


spyOn(component, 'isCurrentStatus').and.callThrough();
component.event = failedEvent;
const statusResult = component.isCurrentStatus('failed');
expect(statusResult).toBeTruthy();



Expected spy on isCurrentStatus to equal true. It because spyOn creates, actually, a spy. And you try smth like expect(Spy).toEqual(Boolean); so you get such error.


Expected spy on isCurrentStatus to equal true.


spyOn


spy


expect(Spy).toEqual(Boolean);



expected undefined to equal true - you get because beforeEach()'s scope is not in your test function (it()) scope


expected undefined to equal true


beforeEach()


it()



As you want to test returned value - you don't need to spy here. Just call function and check its result.



Spy is needed when you need test not return value but something else - for example, it is function of injected dependency but you need to be confident that it was called. So, you create a spy. Or: you need to check how many times was function called, what params were passed etc. Or when you need to mock its behavior.



try this to test the returned value


expect(component.isCurrentStatus('failed')).toEqual(true);



and you can check if the method was called or not


const statusSpy = spyOn(component, 'isCurrentStatus').and.callThrough();
...
expect(statusSpy).toHaveBeenCalledTimes(1);



you can check arguments


expect(statusSpy).toHaveBeenCalledWith('failed')



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế