2017-09-06 1 views
1

나는 테스트하려는 클래스에 다음과 같은 메소드 skipLoggingThisRequest을 가지고있다. 이 메소드는 요청에 따라 true 또는 false을 반환하고 ramda compose을 사용하여 그 값을 얻습니다. 그러나 내 테스트에서 요청 개체에서 설정 한 경로에 관계없이 내 skipLoggingThisRequest 항상 true를 반환합니다.nodejs 클래스에서 ramda 작성하기

무엇이 여기에 있습니까?

내 수업 :

import { compose, filter, join, toPairs, map, prop, flip, contains, test, append } from 'ramda' 
import { create, env } from 'sanctuary' 
import { isEmpty, flattenDeep } from 'lodash' 
import chalk from 'chalk' 
import log from 'menna' 

class MyClass { 

    constructor (headerList) { 
     this.headerWhiteList = flattenDeep(append(headerList, [])); 
    } 

    static getBody (req) { 
     return (!isEmpty(req.body) ? JSON.stringify(req.body) : ''); 
    } 

    static S() { 
     return create({ checkTypes: false, env }); 
    } 

    static isInList() { 
     return flip(contains); 
    } 

    static isInWhitelist() { 
     return compose(this.isInList(this.headerWhiteList), this.S.maybeToNullable, this.S.head); 
    } 

    static parseHeaders() { 
     return (req) => compose(join(','), map(join(':')), filter(this.isInWhitelist), toPairs, prop('headers')); 
    } 

    skipLoggingThisRequest() { 
     return (req) => compose(test(/^.*(swagger|docs|health).*$/), prop('path')) 
    } 

    logger (req, res, next) { 
     if (this.skipLoggingThisRequest(req)) { 
      console.log('Skipping') 
      return next(); 
     } 

     const primaryText = chalk.inverse(`${req.ip} ${req.method} ${req.originalUrl}`); 
     const secondaryText = chalk.gray(`${this.parseHeaders(req)} ${this.getBody(req)}`); 
     log.info(`${primaryText} ${secondaryText}`); 

     return next(); 
    } 
} 

export default MyClass 

내 테스트 :

import sinon from 'sinon'; 
import MyClass from '../lib/MyClass'; 

describe('MyClass',() => { 
    const headerList = ['request-header-1', 'request-header-2']; 

    const request = { 
     'headers': { 
      'request-header-1': 'yabadaba', 
      'request-header-2': 'dooooooo' 
     }, 
     'ip': 'shalalam', 
     'method': 'GET', 
     'originalUrl': 'http://myOriginalUrl.com', 
     'body': '' 
    }; 
    const response = {}; 

    const nextStub = sinon.stub(); 

    describe('Logs request',() => { 
     const myInstance = new MyClass(headerList); 
     const skipLogSpy = sinon.spy(myInstance, 'skipLoggingThisRequest'); 
     request.path = '/my/special/path'; 
     myInstance.logger(request, response, nextStub); 
     sinon.assert.called(nextStub); 
    }); 
}); 

답변

2

this.skipLoggingThisRequest(req) 함수 ((req) => compose(test(/^.*(swagger|docs|health).*$/), prop('path')))을 반환합니다.

부울을 반환하지 않습니다. 그러나 함수는 사실이므로 항상 if 문이 실행됩니다.

가장 가능성있는 작업은 this.skipLoggingThisRequest()(req)입니다. 함수를 얻은 다음 함수에 요청을 적용합니다. 무슨 일이 일어나고 있는지의

데모 :

const testFunction =() => (test) => test === "Hello!"; 
 
console.log(testFunction); 
 
console.log(testFunction()); 
 
console.log(testFunction()("Hello!")); 
 
console.log(testFunction()("Goodbye!")); 
 

 
if (testFunction) { 
 
    console.log("testFunction is truthy."); 
 
} 
 

 
if (testFunction()) { 
 
    console.log("testFunction() is truthy."); 
 
} 
 

 
if (testFunction()("Hello!")) { 
 
    console.log('testFunction()("Hello!") is truthy.'); 
 
} 
 

 
if (!testFunction()("Goodbye!")) { 
 
    console.log('testFunction()("Goodbye!") is falsey.'); 
 
}