2016-09-30 1 views
0

Express js를 사용하는 노드 j에서 단위 테스트를 수행하고 있고 테스트를 위해 저는 mocha를 사용하고 있으며 데이터를 조롱하기 위해 sinon을 사용하고 있습니다. 모든 것은 괜찮습니다. 그러나 문제는 it()에 복수의 어설 션이 포함되어 있고 그 중 하나가 실패한 경우 mocha가 전체 it()이 실패한 것으로 나타나는 경우 테스트 케이스를 실행할 때입니다. 하지만 아무도 실패하더라도 다른 주장이 통과되기를 바란다. 나는 각 필드에 하나씩 it()을 쓰고 싶지 않다.. 내 테스트 코드 당신은 내 userId를 실패해야하며, 사용자 이름을 전달해야하지만 난이 코드를 실행할 때 이/사용자/getCustomerData 실패있어하는 반응을 말한다 여기에서 볼 수 있습니다전체 단위 테스트를 보여주는 모카가 실패했습니다.

//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: '[email protected]', 
      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("[email protected]"); 
     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) 

    }); 

}); 

입니다. 그 대신에 mocha는 userId 필드가 실패하고 userName 필드가 전달되었다고 말해야합니다.

답변

2

모카와 should의 작동 방식이 아닙니다. 어설 션이 실패하면 should은 오류를 던집니다. 즉, 나머지 코드 (후속 어설 션 포함)는 실행되지 않습니다.

요청이 한 번만 수행 될 수 있도록 당신이 테스트를 다시 작성할 수 있지만, 각각의 주장은 여전히 ​​별도로 테스트 :

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("[email protected]"); 
    }); 

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

}); 
+0

을 당신은 내가 각 필드에 대해 하나에게 그것을() 작성해야 의미? 다른 옵션을 사용할 수 있습니까? –

+0

개별 테스트가 실패하거나 통과하는 것을보고 싶다면 별개의'it()'으로 만들어야합니다. AFAIK. – robertklep