如何处理单元测试用例的q和promise
How to handle q and promise for unit test case
我有一个api,它是使用q和promise创建的。我需要为那个api编写单元测试用例。但由于q和承诺,它没有做出回应。这是我的api
exports.test=funtion(req,res)
{
savedata(req.body).saveQ().then(function(result)
{
res.send(result);
});
}
这是我对上述api 的测试用例
var req={'body':{name:'xxxxx'}};
var res={};
describe('savedata',function()
{
it('should save data',function(){
spy=res.send=sinon.spy();
test(req,res);
expect(spy.calledOnce).to.be('true');
});
});
有人能告诉我怎么做吗?
这是不可能进行测试的,因为您没有公开任何方法来知道函数何时完成。你需要打电话的人知道这一点。
我假设这是摩卡单元测试:
exports.test=funtion(req,res){
return savedata(req.body).saveQ().then(function(result){ // note the `return`
res.send(result);
});
};
你可以这样做:
it('should save data',function(){
spy=res.send=sinon.spy();
return test(req,res).then(function(){ // note the return and the then
expect(spy.calledOnce).to.be('true');
});
});
相关文章:
- 你好,这是测试用例,我必须在函数中传递n个参数
- 如何为以下角度函数编写单元测试用例
- 是否有任何开源web应用程序具有良好的QUnit(或JSUnit)测试用例
- 因果报应并没有执行测试用例
- 运行茉莉花测试用例
- 如何阻止量角器在失败时运行进一步的测试用例
- 从FORM访问HTML元素的JavaScript函数的单元测试用例
- Regex单词边界(退格)测试用例
- 针对每个场景使用Jasmine测试用例
- JavaScript/jQuery-脚本不起作用-添加&删除类,合并数组-测试用例
- 你如何为 HTML 输出编写测试用例
- 如何在 eclipse 中为 angularjs 测试和创建测试用例
- Casperjs 多个测试用例
- 不同功能的摩卡测试用例
- 量角器在不使用browser.sleep的情况下编写更干净的测试用例
- 为什么jquery正则表达式每次返回不同的测试用例
- 如何在摩卡的测试用例中调用另一个测试用例
- 函数内 Restangular 的茉莉花测试用例
- 如何创建JavaScript测试用例
- 请建议如何为以下angularjs代码编写Jasmine测试用例