我有以下功能,我想在 jasmine * 中为它编写规格。
function getData(){
return new Promise(function(resolve,reject){
myDB.query('testview/testfn', {key: false, include_docs: true}).then(function(result){
var testdata = result.rows;
if(testdata.length){
resolve(testdata[Math.floor(Math.random() * testdata.length)].doc);
}else{
resolve();
}
}, function(error){
reject(error);
});
});
}
在上面的代码中, myDB 是 pouchDB 的一个实例。基本上我是Jasmine的新手,我需要为上面的函数编写测试。
答案 0 :(得分:0)
我会检查茉莉花中的模仿。例如,您可以阅读jasmine.createSpyObj
(docs)
您可以模拟localDB及其方法:query
。然后,您可以验证该方法是否正确地与模拟交互。
it("Verify getting random song", function() {
localDB = jasmine.createSpyObj('localDB', ['query']);
getRandomSong();
expect(localDB.query).toHaveBeenCalledWith(
'song/downloadStatus',
jasmine.objectContaining({key: false, include_docs: true}));
// more expects.
});