2017-09-06 10 views
1

私はテストしようとしているnode jsクラスの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

機能((req) => compose(test(/^.*(swagger|docs|health).*$/), prop('path')))を返しますthis.skipLoggingThisRequest(req)

ブール値を返しません。ただし、関数は真実であるため、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.'); 
 
}

関連する問題