jish2512
jish2512

Reputation: 91

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 ?

Upvotes: 7

Views: 25487

Answers (2)

Wilmer SH
Wilmer SH

Reputation: 1417

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

Upvotes: 6

James Wright
James Wright

Reputation: 3010

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

Upvotes: 1

Related Questions