在nodejs类中调用ramda compose

时间:2017-09-06 13:52:41

标签: node.js class mocha sinon ramda.js

我正在尝试测试的skipLoggingThisRequest类中有以下方法node js。该方法应根据请求中的路径返回truefalse,使用ramda compose获取该值。但是在我的测试中,无论我在请求对象中设置了什么路径,我的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

我的测试:

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

1 个答案:

答案 0 :(得分: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.');
}