TypeError:无法读取未定义的属性“post”

时间:2016-09-26 12:42:05

标签: node.js should.js

我正在使用express,而且我应该尝试调用我的服务器端点(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);
  });
});

Crud操作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 

我想念一下吗?

1 个答案:

答案 0 :(得分:1)

您必须返回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");