void method testing javascript

感情迁移 提交于 2019-12-30 22:53:56

问题


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 ?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/36402812/void-method-testing-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!