简体   繁体   English

我正在尝试使用mocha和chai测试API返回500(内部服务器错误)的条件

[英]I am trying to test a condition wherein the API returns 500 (internal server error) using mocha and chai

Please, I don't have much of idea of how to go about this. 拜托,我对如何解决这个问题并不了解。 A good explanation of the process will be of great help. 对过程进行良好的解释将有很大帮助。 Thanks in anticipation. 谢谢您的期待。

Here is my controller 这是我的控制器

    async getAllEntries(req, res) {
    try {
      const userId = req.userData.userID;

      const query = await client.query(
        `SELECT * FROM entries WHERE user_id=($1) ORDER BY entry_id ASC;`, [
          userId,
        ],
      );
      const entries = query.rows;
      const count = entries.length;

      if (count === 0) {
        return res.status(200).json({
          message: 'There\'s no entry to display',
        });
      }

      return res.status(200).json({
        message: "List of all entries",
        "Number of entries added": count,
        entries,
      });
    } catch (error) {
      return res.status(500).json({
        message: "Error processing request",
        error,
      });
    }
  }

For this case, what I'm going to do is to make the client.query process failed. 对于这种情况,我要做的是使client.query进程失败。 So, based on your code, it will go to catch statement. 因此,根据您的代码,它将转到catch语句。

const chai = require('chai');
const assert = chai.assert;
const sinon = require('sinon');

const client = require('...'); // path to your client library
const controller = require('...'); // path to your controller file

describe('controller test', function() {
  let req;
  let res;

  // error object to be used in rejection of `client.query`
  const error = new Error('something weird');

  beforeEach(function() {
    req = sinon.spy();

    // we need to use `stub` for status because it has chain method subsequently
    // and for `json` we just need to spy it
    res = {
      status: sinon.stub().returnsThis(),
      json: sinon.spy()
    };

    // here we reject the query with specified error
    sinon.stub(client, 'query').rejects(error);
  });

  afterEach(function() {
    sinon.restore();
  })

  it('catches error', async function() {    
    await controller.getAllEntries(req, res);

    // checking if `res` is called properly
    assert(res.status.calledWith(500));
    assert(res.json.calledWith({
      message: 'Error processing request',
      error
    }));
  });
});

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何测试使用Mocha,Sinn和Chai返回的函数? - How do I test a function which returns a function using Mocha, Sinon, and Chai? 我正在尝试使用 mocha 运行我的测试脚本,但出现“参考错误:beforeEach 未定义”的错误 - I am trying to run my test script using mocha but there is an error of "Reference Error: beforeEach is not defined" 使用 Chai 和 mocha 的测试用例 - Test cases using Chai and mocha 每当我尝试发布此错误时,都会显示“加载资源失败:服务器响应状态为 500(内部服务器错误)” - whenever i am trying to post this error is showing "Failed to load resource: the server responded with a status of 500 (Internal Server Error) " 我面临 500 内部服务器错误! 对于 ajax 代码 - i am facing 500 Internal server error! for a ajax code GET方法返回500内部服务器错误 - GET method returns 500 Internal server error Mocha / Chai:测试精确的投掷错误结果 - Mocha/Chai: Test for exact throw error result Mocha测试没有使用Meteor运行Chai断言 - Mocha test not running Chai assertion using Meteor 使用mocha和chai测试作为数组的输出 - Using mocha and chai to test outputs which are arrays 如果需要登录,如何在Node.js服务器端使用Mocha Chai测试RESTful CRUD api? - How to test RESTful CRUD api with Mocha Chai on Node.js server side if login needed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM