TypeError: Cannot read property 'post' of undefined
TypeError: Cannot read property 'post' of undefined
我正在使用 express 和 should,我正在尝试调用我的服务器端点 (app.js) 来测试结果。
我的服务器文件(app.js):
app.post('/customer', authorize({allowedPowerLevel: 50}), function(request, response, callback) {
const customerType = parseInt(request.body.customerType);
const data = JSON.parse(request.body.data);
});
});
粗加工操作index.js 文件(post()定义):
let server = supertest.agent("http://localhost:5003");
let loggedInUserManager = require("./../loggedInUser")();
let idProvider = require("./../id-provider")();
let jsonProvider = require("./../json-provider")();
module.exports = function () {
const crudOperations = {
post: function(idCustomerType, callback) {
server
.post("/customer")
.set("x-access-token", loggedInUserManager.authentication.token)
.send({
customerType: idCustomerType,
data: JSON.stringify(jsonProvider.getTemplate("individual"))
})
.end(function(error, response) {
callback(response);
});
}
}
}
crudOperations post() 调用
let should = require("should");
let crudCustomer = require("./crud-customer")();
let startTests = function(idCustomerType) {
describe(`Post customer`, function () {
let returnedCustomerId;
it(`should post a new customer`, function(done) {
crudCustomer.post(idCustomerType, function(response) {
should(response.status).be.eql(200);
should(response.body).have.property("customerId").not.be.eql("");
returnedCustomerId = response.body.idCustomer;
done();
});
});
});
尝试 运行 测试时出现以下错误:
Post customer should post a new customer:
TypeError: Cannot read property 'post' of undefined
at Context.<anonymous> (C:\Users\Alex\Desktop\projects\watson\server\tests\customer.js:15:19)
at callFnAsync
我错过了什么吗?
您必须 return crudOperations 对象。类似的东西应该可以工作:
let server = supertest.agent("http://localhost:5003");
let loggedInUserManager = require("./../loggedInUser")();
let idProvider = require("./../id-provider")();
let jsonProvider = require("./../json-provider")();
module.exports = function () {
const crudOperations = {
post: function (idCustomerType, callback) {
server
.post("/customer")
.set("x-access-token", loggedInUserManager.authentication.token)
.send({
customerType: idCustomerType,
data: JSON.stringify(jsonProvider.getTemplate("individual"))
})
.end(function (error, response) {
callback(response);
});
}
}
return crudOperations;
}
否则只需将 crudOperations 对象分配给 module.exports,类似于:
const crudOperations = {
post: function (idCustomerType, callback) {
server
.post("/customer")
.set("x-access-token", loggedInUserManager.authentication.token)
.send({
customerType: idCustomerType,
data: JSON.stringify(jsonProvider.getTemplate("individual"))
})
.end(function (error, response) {
callback(response);
});
}
}
module.exports = crudOperations;
但是如果您采用这种方法,那么您必须像这样在测试中要求文件:
let crudCustomer = require("./crud-customer");
我正在使用 express 和 should,我正在尝试调用我的服务器端点 (app.js) 来测试结果。
我的服务器文件(app.js):
app.post('/customer', authorize({allowedPowerLevel: 50}), function(request, response, callback) {
const customerType = parseInt(request.body.customerType);
const data = JSON.parse(request.body.data);
});
});
粗加工操作index.js 文件(post()定义):
let server = supertest.agent("http://localhost:5003");
let loggedInUserManager = require("./../loggedInUser")();
let idProvider = require("./../id-provider")();
let jsonProvider = require("./../json-provider")();
module.exports = function () {
const crudOperations = {
post: function(idCustomerType, callback) {
server
.post("/customer")
.set("x-access-token", loggedInUserManager.authentication.token)
.send({
customerType: idCustomerType,
data: JSON.stringify(jsonProvider.getTemplate("individual"))
})
.end(function(error, response) {
callback(response);
});
}
}
}
crudOperations post() 调用
let should = require("should");
let crudCustomer = require("./crud-customer")();
let startTests = function(idCustomerType) {
describe(`Post customer`, function () {
let returnedCustomerId;
it(`should post a new customer`, function(done) {
crudCustomer.post(idCustomerType, function(response) {
should(response.status).be.eql(200);
should(response.body).have.property("customerId").not.be.eql("");
returnedCustomerId = response.body.idCustomer;
done();
});
});
});
尝试 运行 测试时出现以下错误:
Post customer should post a new customer:
TypeError: Cannot read property 'post' of undefined
at Context.<anonymous> (C:\Users\Alex\Desktop\projects\watson\server\tests\customer.js:15:19)
at callFnAsync
我错过了什么吗?
您必须 return crudOperations 对象。类似的东西应该可以工作:
let server = supertest.agent("http://localhost:5003");
let loggedInUserManager = require("./../loggedInUser")();
let idProvider = require("./../id-provider")();
let jsonProvider = require("./../json-provider")();
module.exports = function () {
const crudOperations = {
post: function (idCustomerType, callback) {
server
.post("/customer")
.set("x-access-token", loggedInUserManager.authentication.token)
.send({
customerType: idCustomerType,
data: JSON.stringify(jsonProvider.getTemplate("individual"))
})
.end(function (error, response) {
callback(response);
});
}
}
return crudOperations;
}
否则只需将 crudOperations 对象分配给 module.exports,类似于:
const crudOperations = {
post: function (idCustomerType, callback) {
server
.post("/customer")
.set("x-access-token", loggedInUserManager.authentication.token)
.send({
customerType: idCustomerType,
data: JSON.stringify(jsonProvider.getTemplate("individual"))
})
.end(function (error, response) {
callback(response);
});
}
}
module.exports = crudOperations;
但是如果您采用这种方法,那么您必须像这样在测试中要求文件:
let crudCustomer = require("./crud-customer");