【问题标题】:How can I mock pouchDB documents?如何模拟 pouchDB 文档?
【发布时间】:2016-06-01 17:24:08
【问题描述】:

我有以下函数,我想在 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 的新手,我需要为上述功能编写测试。

【问题讨论】:

    标签: javascript jasmine automated-tests pouchdb


    【解决方案1】:

    我会检查 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.
    });
    

    【讨论】:

    • 非常感谢@laroslav
    • 我会考虑至少两种方法。 1.如果回调是多行函数-尝试将其提取到单独的方法中,然后单独测试它们。 2. 你可以解决promise并检查结果。这是一个要提臀的帖子:blog.pivotal.io/labs/labs/testing-javascript-promises
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2011-08-25
    • 2016-05-20
    相关资源
    最近更新 更多