Node.js 在nodejs类中调用ramda compose
在我试图测试的Node.js 在nodejs类中调用ramda compose,node.js,class,mocha.js,sinon,ramda.js,Node.js,Class,Mocha.js,Sinon,Ramda.js,在我试图测试的节点js类中,我有以下方法skipLoggingThisRequest。该方法应该根据请求中的路径返回true或false,使用ramda compose获得该值。然而,在我的测试中,无论我在请求对象中设置了什么路径,我的skipLoggingThisRequest总是返回true 我错过了什么 我的班级: import { compose, filter, join, toPairs, map, prop, flip, contains, test, append } from
节点js
类中,我有以下方法skipLoggingThisRequest
。该方法应该根据请求中的路径返回true
或false
,使用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);
});
});
this.skiploggingthis请求(req)
返回一个函数((req)=>compose(test(/^.*(swagger | docs | health)。*$/),prop('path'))
)
它不返回布尔值。然而,由于函数是真实的,所以总是执行if
语句
您最可能想做的是this.skipLoggingThisRequest()(req)
。获取函数,然后对其应用请求
演示正在发生的事情:
consttestfunction=()=>(test)=>test===“你好!”;
log(testFunction);
log(testFunction());
log(testFunction()(“Hello!”);
log(testFunction()(“再见!”);
if(testFunction){
log(“testFunction是truthy.”);
}
if(testFunction()){
log(“testFunction()是真实的。”);
}
if(testFunction()(“Hello!”){
log('testFunction()(“Hello!”)是真实的。');
}
如果(!testFunction()(“再见!”){
log('testFunction()(“再见!”)是错误的。');
}