void方法测试javascript [英] void method testing javascript

查看:101
本文介绍了void方法测试javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用javascript编写的方法,我使用Jasmine来测试方法。
该方法是一个void类型,它调用另一个方法。



我必须测试方法是否正在调用另一个方法,本方法返回void 。



我应该在expect子句中写什么来比较它。

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

Jasmine代码:

  describe('unit test void method',function(){
it(sendMessage方法应该调用postMessage,function(){
expect(sendMessage(hello);
})
})



<我应该将它与什么进行比较?

解决方案

詹姆斯是对的。这是一个间谍功能,虽然我使用了另一种方法。 / p>

在您的设置之前的某个地方,每个函数:

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

YourObject 是任何包含该函数的对象。



期望:

  it('期望postMessage()被调用',function(){

//调用此函数
YourObject.sendMessage();

//检查内部函数
expect(YourObj ect.postMessage).toHaveBeenCalled();
});


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 ?

解决方案

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.sendMessage();

    // Check internal function
    expect(YourObject.postMessage).toHaveBeenCalled();
});

这篇关于void方法测试javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆