Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

jasmine - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...