代码之家  ›  专栏  ›  技术社区  ›  Dave Chambers

Sinon存根发出意外的未处理的PromiseRejection警告

  •  1
  • Dave Chambers  · 技术社区  · 8 年前

    我有这样的功能:

        static async performDatabaseConnectivityHealthCheck(logger) {
    
        let con;
        let ret;
    
        try {
    
            con = mysql.createConnection({
                host: config.db.host,
                user: config.db.user,
                password: config.db.password,
                database: config.db.db,
            });
    
            const { retData } = await sqlFileReader.read('./src/database/sql/healthcheck.sql', [config.db.db], con, logger);
    
            // Do something with retData.....
    
        }
        catch (error) {
            logger.error(error.message);
            throw error;
        }
        finally {
            if (con) {
                con.end();
            }
        }
        return ret;
    
    }
    

    像这样的测试:

        it('throws a \'Database Healthcheck Error\' error when the Database Healthcheck gives the wrong data', async () => {
    
    //Some irrelevant details here including providing rars for rars.logger in the following...
    
        sandbox.stub(sqlFileReader, 'read').returns(Promise.reject(new Error("ER_PROCACCESS_DENIED_ERROR: execute command denied to user 'dchambers'@'%' for routine 'uk_txtloan.connection_ids'")));
    
        expect(async () => {
            await RiskAffordabilityReportService.performDatabaseConnectivityHealthCheck(rars.logger);
        }).to.throw('ER_PROCACCESS_DENIED_ERROR: execute command denied to user \'dchambers\'@\'%\' for routine \'uk_txtloan.connection_ids\'');
    
    });
    

    我有两个问题。首先, sandbox.stub 行给出了以下警告,我很困惑,因为我要求承诺被拒绝!

    UnhandledPromiseRejectionWarning: Error: ER_PROCACCESS_DENIED_ERROR: execute command denied to user 'dchambers'@'%' for routine 'uk_txtloan.connection_ids'

    其次,测试没有通过:

    AssertionError: expected [Function] to throw an error .

    我主要想知道这个警告。我尝试了以下语法,它们给出了相同的错误:

    sandbox.stub(sqlFileReader, 'read').throws(new Error("ER_PROCACCESS_DENIED_ERROR: execute command denied to user 'dchambers'@'%' for routine 'uk_txtloan.connection_ids'"));

    sandbox.stub(sqlFileReader, 'read').resolves(Promise.reject(new Error("ER_PROCACCESS_DENIED_ERROR: execute command denied to user 'dchambers'@'%' for routine 'uk_txtloan.connection_ids'")));

    sandbox.stub(sqlFileReader, 'read').rejects(new Error("ER_PROCACCESS_DENIED_ERROR: execute command denied to user 'dchambers'@'%' for routine 'uk_txtloan.connection_ids'"));

    消除警告的正确方法是什么?奖励:通过考试。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Estus Flask    8 年前

    最近引发了未处理的承诺拒绝错误 Promise 未处理拒绝承诺的实现,即未与 catch(...) then(..., ...) .

    to.throw 断言用包装函数 try..catch ,但未引发任何错误 async 功能。 异步的 函数是返回承诺的函数的语法糖。内部未捕获错误 异步的 函数导致返回被拒绝的承诺。相反,它应该作为承诺的断言与chai一起断言:

    expect(RiskAffordabilityReportService.performDatabaseConnectivityHealthCheck(rars.logger))
    .to.be.rejectedWith(
      Error,
      'ER_PROCACCESS_DENIED_ERROR: execute command denied to user \'dchambers\'@\'%\' for routine \'uk_txtloan.connection_ids\''
    );