调用ramda组成nodejs类

我有以下方法skipLoggingThisRequest在我试图testingnode js类。 该方法应该返回truefalse ,根据请求中的path,使用ramda compose来获得该值。 但是在我的testing中,无论我在请求对象中设置了什么path,我的skipLoggingThisRequest总是返回true。

我在这里错过了什么?

我的课:

 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 

我的testing:

 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); }); }); 

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.'); }