2017-02-01 1 views
1

HTTP GET 요청을 처리하는 Restify 프로젝트에 함수가 있습니다. 처리가 끝나면 Sequelize를 사용하여 현재 세션에 대한 사용자 엔터티를 찾습니다. User.findOne 함수는 약속을 반환하고 그 약속의 결과에 따라, 나는 난 그래서 시험에 도움이되는 몇 가지 라이브러리를 시도했습니다 (200) 또는 (404)HTTP 처리기에서 비동기 코드를 테스트하려면 어떻게해야합니까?

static getMe(req, res, next) { 
    const userInfo = BaseController.getUserSession(req); 

    // hard to test this part 
    User.findOne({ 
     where: {email: userInfo.email} 
    }).then(function(user) { 
     if (user) BaseController.respondWith200(res, user); 
     else BaseController.respondWith404(res, 'User not found.'); 
    }, function(error) { 
     BaseController.respondWith404(res, error); 
    }).then(function() { 
     return next(); 
    }); 
} 

와 HTTP 응답을 보내고있다 이것이 지저분한 것들의 조합이라면 미안합니다.

const usersFixture = [ 
    {id:2, email:'[email protected]', facebookId:54321, displayName: 'Ozzy Osbourne'}, 
    {id:3, email:'[email protected]', facebookId:34521, displayName: 'Zakk Wylde'}, 
    {id:4, email:'[email protected]', facebookId:12453, displayName: 'John Lennon'} 
]; 

this.findOneSpy = sinon.spy(function(queryObj) { 
    return new Promise(function(resolve, reject) { 
    const user = usersFixture.find(function(el) { return el.email === queryObj.where.email }); 
    if (user) resolve(user); 
    else resolve(null); 
    }); 
}); 

this.respondWith200Spy = sinon.spy(function(res, data) {}); 
this.respondWith400Spy = sinon.spy(function(res, error) {}); 
this.respondWith404Spy = sinon.spy(function(res, error) {}); 

this.controller = proxyquire('../../controllers/user-controller', { 
    '../models/user': { 
    findOne: this.findOneSpy 
    }, 
    './base-controller': { 
    respondWith200: this.respondWith200Spy, 
    respondWith400: this.respondWith400Spy, 
    respondWith404: this.respondWith404Spy 
    } 
}); 

그리고 여기처럼 내 테스트 중 하나가 모습입니다 : 이것은 내 테스트를 위해 내 beforeEach 기능에 우리가 실제로 함수와 함수에 콜백을 통과하지 않기 때문에

it('should return 200 with user data if user email matches existing user', function() { 
    // THIS FUNCTION IS NEVER HIT 
    this.respondWith200Spy = function(res, data) { 
    data.should.equal({id:4, email:'[email protected]', facebookId:12453, displayName: 'John Lennon'}); 
    done(); 
    }; 
    const req = {session:{user:{email:'[email protected]'}}}; 
    this.controller.getMe(req, this.res, this.nextSpy); 
    this.findOneSpy.should.have.been.called; 
}); 

실제로 아무 것도 반환하지 않습니다 (다른 곳에서는 비동기식 작업을 수행함). 올바르게 작동하는지 테스트하기 위해 어떻게 테스트해야 하는지를 알 수 없습니다. 어떤 도움을 주셔서 감사합니다.

실제 코드는 정상적으로 작동합니다. 나는 그 프로젝트에 몇 가지 양질의 단위 테스트를하려고하고있다. 감사!

답변

0

나는 proxyquire을 사용하여이를 수행하는 방법을 찾았습니다. 방금 테스트 한 컨트롤러 클래스를 다시 작성하여 respondWith200 콜백을 어설 션으로 만들었습니다. 그런 다음 함수에 대한 새 스파이를 만들었습니다.이 스파이는 done (테스트 사례로 전달됨)을 호출합니다. 나는 그 코드가 모두 타격을 받고 있음을 확인했다.

it('should return 200 with user data if user email matches existing user', function(done) { 
    const controller = proxyquire('../../controllers/user-controller', { 
    '../models/user': { 
     findOne: this.findOneSpy 
    }, 
    './base-controller': { 
     respondWith200: function(res, data) { 
     data.displayName.should.equal('John Lennon'); 
     }, 
     respondWith400: this.respondWith400Spy, 
     respondWith404: this.respondWith404Spy 
    } 
    }); 
    const req = {grft_session:{user:{email:'[email protected]'}}}; 
    const nextSpy = sinon.spy(function() { 
    done(); 
    }); 
    controller.getMe(req, this.res, nextSpy); 
    this.findOneSpy.should.have.been.called; 
}); 
관련 문제