Node unit tests - TypeError: Cannot read property 'order' of undefined
Node unit tests - TypeError: Cannot read property 'order' of undefined
我收到以下错误,Error: BLANKET-Error parsing instrumented code: TypeError: Cannot read property 'order' of undefined
在我的测试代码中指示 testedModule = proxyquire('../index', {
.
所在的行
谁能看出我为什么会收到此错误消息?
我的测试文件:
pattern: function (filename) {
return !/node_modules/.test(filename);
}
});
// in terminal, type the following command to get code coverage: mocha -R html-cov > coverage.html
var chai = require('chai');
var sinonChai = require("sinon-chai");
var sinon = require('sinon');
chai.use(sinonChai);
var proxyquire = require('proxyquire').noCallThru();
var expect = chai.expect;
var mockDir = require('mock-fs');
var extend = require('lodash').extend;
describe("Invoicer", function () {
describe("Success call", function () {
var fileReader = require('../fileReader');
var getObject = require('../getObject');
var sendEmail = require('../sendEmail');
var baseEvent = {
order: "http://orange/order/1234",
email: '<mario@hevnly.com>'
};
var testedModule, fakePath, fakeTemplate, fakeData, fakeMessage, eventObj, fileReaderStub, getObjectStub, sendEmailStub, callbackSpy;
before(function () {
eventObj = extend({}, baseEvent);
fileReaderStub = sinon.stub(fileReader, 'read');
getObjectStub = sinon.stub(getObject, '_get');
sendEmailStub = sinon.stub(sendEmail, '_send');
callbackSpy = sinon.spy();
testedModule = proxyquire('../index', {
'./fileReader': {
'read': fileReaderStub
},
'./getObject': {
'_get': getObjectStub
},
'./sendEmail': {
'_send': sendEmailStub
}
});
fakePath = "path/to/file.html";
fakeMessage = "Email sent";
fakeTemplate = "WhatcanIsay";
fakeData = "Hello World";
getObjectStub.callsArgWith(1, null, fakeData);
fileReaderStub.callsArgWith(2, null, fakeTemplate);
sendEmailStub.callsArgWith(2, null, fakeMessage);
testedModule.Invoicer(eventObj, {done: function (error, result) {
console.log("Here!");
callbackSpy.apply(null, arguments);
}});
});
after(function () {
fileReader.read.restore();
getObject._get.restore();
sendEmail._send.restore();
});
it("context.done is called with no error", function () {
expect(callbackSpy).has.been.called;
});
});
});
这是index.js
'use strict';
var async = require('async');
var obj = require("./config.json");
var fileReader = require('./fileReader');
var _readFile = fileReader.read;
var getObject = require('./getObject');
var _getObject = getObject._get;
var sendEmail = require('./sendEmail');
var _sendEmail = sendEmail._send;
exports.Invoicer = function (event, context) {
console.log("Order: %s", event.order);
console.log("Email: %s", event.email);
var urlPath = event.order; // process.argv[2]; // The url for the order endpoint
var _recipient = event.email; //process.argv[3]; // The recipient of the email
var _filePath = obj.filePath;
async.waterfall([
function retrieveObject (next) {
console.log("waterfall getobject");
_getObject(urlPath, next);
},
function _readF (data, next) {
console.log("waterfall reafile");
_readFile(data, _filePath, next);
},
function sendsEmail (popHtml, next) {
console.log("waterfall sendemail");
_sendEmail(popHtml, _recipient, next);
}
], function (error, result) {
if (error) {
console.log("Error waterfall");
return;
context.done(error);
} else {
console.log("Done!");
return;
context.done(result);
}
});
};
if (!process.env.LAMBDA_TASK_ROOT) {
exports.Invoicer();
}
此代码路径是否执行过?如果是这样,它将抛出该错误,因为您没有传递任何参数,因此 event.order
将导致 TypeError: undefined
if (!process.env.LAMBDA_TASK_ROOT) {
exports.Invoicer();
}
我收到以下错误,Error: BLANKET-Error parsing instrumented code: TypeError: Cannot read property 'order' of undefined
在我的测试代码中指示 testedModule = proxyquire('../index', {
.
谁能看出我为什么会收到此错误消息?
我的测试文件:
pattern: function (filename) {
return !/node_modules/.test(filename);
}
});
// in terminal, type the following command to get code coverage: mocha -R html-cov > coverage.html
var chai = require('chai');
var sinonChai = require("sinon-chai");
var sinon = require('sinon');
chai.use(sinonChai);
var proxyquire = require('proxyquire').noCallThru();
var expect = chai.expect;
var mockDir = require('mock-fs');
var extend = require('lodash').extend;
describe("Invoicer", function () {
describe("Success call", function () {
var fileReader = require('../fileReader');
var getObject = require('../getObject');
var sendEmail = require('../sendEmail');
var baseEvent = {
order: "http://orange/order/1234",
email: '<mario@hevnly.com>'
};
var testedModule, fakePath, fakeTemplate, fakeData, fakeMessage, eventObj, fileReaderStub, getObjectStub, sendEmailStub, callbackSpy;
before(function () {
eventObj = extend({}, baseEvent);
fileReaderStub = sinon.stub(fileReader, 'read');
getObjectStub = sinon.stub(getObject, '_get');
sendEmailStub = sinon.stub(sendEmail, '_send');
callbackSpy = sinon.spy();
testedModule = proxyquire('../index', {
'./fileReader': {
'read': fileReaderStub
},
'./getObject': {
'_get': getObjectStub
},
'./sendEmail': {
'_send': sendEmailStub
}
});
fakePath = "path/to/file.html";
fakeMessage = "Email sent";
fakeTemplate = "WhatcanIsay";
fakeData = "Hello World";
getObjectStub.callsArgWith(1, null, fakeData);
fileReaderStub.callsArgWith(2, null, fakeTemplate);
sendEmailStub.callsArgWith(2, null, fakeMessage);
testedModule.Invoicer(eventObj, {done: function (error, result) {
console.log("Here!");
callbackSpy.apply(null, arguments);
}});
});
after(function () {
fileReader.read.restore();
getObject._get.restore();
sendEmail._send.restore();
});
it("context.done is called with no error", function () {
expect(callbackSpy).has.been.called;
});
});
});
这是index.js
'use strict';
var async = require('async');
var obj = require("./config.json");
var fileReader = require('./fileReader');
var _readFile = fileReader.read;
var getObject = require('./getObject');
var _getObject = getObject._get;
var sendEmail = require('./sendEmail');
var _sendEmail = sendEmail._send;
exports.Invoicer = function (event, context) {
console.log("Order: %s", event.order);
console.log("Email: %s", event.email);
var urlPath = event.order; // process.argv[2]; // The url for the order endpoint
var _recipient = event.email; //process.argv[3]; // The recipient of the email
var _filePath = obj.filePath;
async.waterfall([
function retrieveObject (next) {
console.log("waterfall getobject");
_getObject(urlPath, next);
},
function _readF (data, next) {
console.log("waterfall reafile");
_readFile(data, _filePath, next);
},
function sendsEmail (popHtml, next) {
console.log("waterfall sendemail");
_sendEmail(popHtml, _recipient, next);
}
], function (error, result) {
if (error) {
console.log("Error waterfall");
return;
context.done(error);
} else {
console.log("Done!");
return;
context.done(result);
}
});
};
if (!process.env.LAMBDA_TASK_ROOT) {
exports.Invoicer();
}
此代码路径是否执行过?如果是这样,它将抛出该错误,因为您没有传递任何参数,因此 event.order
将导致 TypeError: undefined
if (!process.env.LAMBDA_TASK_ROOT) {
exports.Invoicer();
}