Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何正确地存根一个承诺对象,它是另一个承诺的一部分_Javascript_Promise_Mocha.js_Sinon_Pg Promise - Fatal编程技术网

Javascript 如何正确地存根一个承诺对象,它是另一个承诺的一部分

Javascript 如何正确地存根一个承诺对象,它是另一个承诺的一部分,javascript,promise,mocha.js,sinon,pg-promise,Javascript,Promise,Mocha.js,Sinon,Pg Promise,我有一个promise函数,它基于客户端cookie执行身份验证 const getInitialState = (id_token) => { let initialState; return new Promise((resolve,reject) => { if(id_token == null){ initialState = {userDetails:{username: 'Anonymous',isAuthenticated: false}}

我有一个promise函数,它基于客户端cookie执行身份验证

const getInitialState = (id_token) => {
  let initialState;
  return new Promise((resolve,reject) => {
    if(id_token == null){
      initialState = {userDetails:{username: 'Anonymous',isAuthenticated: false}}
      resolve(initialState)
    }else{
        var decoded = jwt.verify(JSON.parse(id_token),'rush2112')
        db.one('SELECT  * FROM account WHERE account_id = $1',decoded.account_id)
          .then(function(result){
            console.log('result is : ',result)
            initialState = {userDetails:{username:result.username,isAuthenticated:true}}
            resolve(initialState)
          })
          .catch(function(err){
            console.log('There was something wrong with the token',e)
            reject('There was an error parsing the token')
          })
    }
  })
}
getInitialState是一个promise对象,如果cookie有效,它将调用数据库函数(另一个promise对象)

我想在这里存根db调用以解析为用户名。但不管我怎么尝试,它都不起作用

我尝试了两个库
sinonsubpromise
sinon-as-promised
。但是这两个函数似乎都会导致超时错误,这告诉我,
db
函数没有得到解决

我认为我没有正确地中断db函数

这些是我尝试过的各种方法

stub2 = sinon.stub(db,'one')

stub2.returnsPromise().resolves({username:'Kannaj'})

所有这些都会导致摩卡超时错误

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
这是我的全部测试功能

  it('should return a valid user if id_token is valid',function(){
    id_token = '{"account_id":1}'
    console.log('stub1: ',stub1(), typeof(stub1))
    console.log('stub2 : ',stub2,typeof(stub2))

    // my attempts here
    return expect(getInitialState(id_token)).to.eventually.be.true
  })

出于某种原因,我认为摩卡/锡诺在调用db.any时就失去了pg承诺上下文。不知道为什么。

我不能像承诺的那样与sinon或SinonSubpromise交谈,但你不需要他们来完成这样的事情

const sinon = require('sinon');
const chai = require('chai');
chai.use(require('chai-as-promised'));
const expect = chai.expect;

// real object
const db = {
  one: function () {
    // dummy function
  }
};

// real function under test
function foo () {
  return db.one('SELECT * FROM account WHERE account_id = $1');
}

describe('foo', function () {
  beforeEach(function () {
    sinon.stub(db, 'one')
      .withArgs('SELECT * FROM account WHERE account_id = $1')
      .returns(Promise.resolve({username: 'Kannaj'}));
  });

  it('should not timeout', function () {
    return expect(foo())
      .to
      .eventually
      .eql({username: 'Kannaj'});
  });

  afterEach(function () {
    db.one.restore();
  });
});

必须是存根的东西,因为
pgpromise
本身提供100%的测试覆盖率,没有问题。你见过它测试自己的方式吗?也许这会有帮助;)我不知道为什么它仍然给我一个错误。。可能是db.one本身就是一个promise对象,而主promise函数无法捕获.then方法吗?我遇到了同样的问题,我尝试了这个方法。仍然得到超时。有人设法解决了这个问题吗?
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
  it('should return a valid user if id_token is valid',function(){
    id_token = '{"account_id":1}'
    console.log('stub1: ',stub1(), typeof(stub1))
    console.log('stub2 : ',stub2,typeof(stub2))

    // my attempts here
    return expect(getInitialState(id_token)).to.eventually.be.true
  })
const sinon = require('sinon');
const chai = require('chai');
chai.use(require('chai-as-promised'));
const expect = chai.expect;

// real object
const db = {
  one: function () {
    // dummy function
  }
};

// real function under test
function foo () {
  return db.one('SELECT * FROM account WHERE account_id = $1');
}

describe('foo', function () {
  beforeEach(function () {
    sinon.stub(db, 'one')
      .withArgs('SELECT * FROM account WHERE account_id = $1')
      .returns(Promise.resolve({username: 'Kannaj'}));
  });

  it('should not timeout', function () {
    return expect(foo())
      .to
      .eventually
      .eql({username: 'Kannaj'});
  });

  afterEach(function () {
    db.one.restore();
  });
});