Void javascript testing method

I have a method written in javascript and I use Jasmine to test the method. This method is a void type that calls another method.

I need to check if a method calls another method, the real method returns void.

what should I write in the expect clause to compare it.

sendMessage=function(data){ if(data!=null) { postMessage(data); } } 

Jasmine Code:

 describe('unit test void method', function(){ it("sendMessage method should invoke the postMessage", function () { expect(sendMessage("hello"); }) }) 

What should I compare with him?

+3
source share
2 answers

James is right. This is a spy function, although I use a different approach.

Somewhere in your setup beforeEach function:

 spyOn(YourObject, 'postMessage').and.callThrough(); 

YourObject is any object that contains a function.

Expectations:

 it('expects postMessage() to have been called', function () { // make the call to this function YourObject.postMessage(); // Check internal function expect(YourObject.postMessage).toHaveBeenCalled(); }); 
+2
source

It looks like you want to use Jasmine spies to track when and how often the method is called:

 expect(obj.method.calls.any()).toBe(true); 
0
source

Source: https://habr.com/ru/post/1012344/


All Articles