我如何模拟 pouchDB 文档?
How can I mock pouchDB documents?
我有以下功能,我想在 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 的新手,我需要为上述功能编写测试。
我会检查 Jasmine 中的 Mocking。例如,您可以阅读 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.
});
我有以下功能,我想在 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 的新手,我需要为上述功能编写测试。
我会检查 Jasmine 中的 Mocking。例如,您可以阅读 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.
});