显示整个单元测试的摩卡失败

时间:2016-09-30 11:34:09

标签: node.js unit-testing mocha sinon chai

我正在使用Express js在Node js中进行单元测试,并且我正在使用mocha进行测试,并且我正在使用sinon来模拟数据。一切都很好,但我的问题是当我运行测试用例时it()包含多个断言而其中任何一个失败然后mocha显示整个it()失败。但我希望其他断言即使任何一个失败也能通过。 我不想为每个字段写一个it()。我的测试代码是

//loading testing dependencies
var request = require('supertest');
var server = require('./app');
var chai = require('chai');
var chaiHttp = require('chai-http');
var sinon = require("sinon");
var should = chai.should();

//configuring chai
chai.use(chaiHttp);

//ORM controller (we need to mock data in it's method)
var rootController = require('./app/controllers/users/users_controller');

//Writing test cases
describe('loading express', function () {
    //mock data before each request
    before(function(){
      //select the method of the ORM controller which you want to mock
      sinon.stub(rootController, "get", //get is the method of ORM's customers_controller'
      function(req, res, next){
        //response object which we are going to mock
          var response = {};
          response.status = 'success',
          response.data = {
            userId: '0987654321@ef',
            userName:'John'
          };
          next(response);
      });
    });
  it('responds to /users/getUserData', function testMethod(done) {
    //call server file (app.js)
    request(server)
      //send request to the Express route which you want to test
      .get('/users/getUserData?id=0987654321')
      //write all expactions here
      .expect(200)
      .end(function(err, res){
        console.log("Generated response is ", res.body);
        res.should.have.status(200);
        res.body.should.be.a('object');
        //res.body.status.should.equal("success");
        res.body.data.userId.should.equal("0987654321@ef347389");
        res.body.data.userName.should.equal("John");
        //done is the callback of mocha framework
        done();
      });

  });

  it('responds to /', function testSlash(done) {
    request(server)
      .get('/')
      .expect(200, done);
  });

  it('404 everything else', function testPath(done) {
    request(server)
      .get('/foo/bar')
      .expect(404, done)

  });

});

你可以在这里看到我的userId应该失败并且应该传递userName但是当我运行这段代码时它会说响应/ users / getCustomerData 失败了。而不是mocha应该说userId字段失败并且userName字段已经通过。

1 个答案:

答案 0 :(得分:2)

这不是Mocha和should的工作方式:当断言失败时,should会抛出错误,这意味着其余的代码(包括任何后续的断言)都不会被执行。 / p>

您可以重写测试,以便请求只进行一次,但每个断言仍然是单独测试的:

describe('responds to /users/getUserData', function testMethod(done) {
  let reqErr, reqRes;

  before(function(done) {
    request(server)
      .get('/users/getUserData?id=0987654321')
      .expect(200)
      .end(function(err, res) {
        reqErr = err;
        reqRes = res;
        done();
      });
  });

  it('should have the correct body type', function() {
    reqRes.body.should.be.a('object');
  });

  it('should have the correct userId', function() {
    reqRes.body.data.userId.should.equal("0987654321@ef347389");
  });

  it('should have the correct userName', function() {
    reqRes.body.data.userName.should.equal("John");
  });

});