void method testing javascript

I have a method written in javascript and I am using Jasmine to test the method. The method is a void type which is invoking another method .

I have to test if the method is invoking the other method, the present method is returning 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 it with ?

Answers:

Answer

James is right. That's a spy function, although I use another approach.

Somewhere in your setup beforeEach function:

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

YourObject being whatever object contains the 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();
});
Answer

Sounds like you want to use Jasmine spies to track when and how often a method is called:

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.