【问题标题】:Error using `should` to test nodeJS app ( undefined is not a function )使用 `should` 测试 nodeJS 应用程序时出错(未定义不是函数)
【发布时间】:2015-10-15 23:34:17
【问题描述】:

我想使用 mocha 测试和 REST API,所以我有以下代码来测试创建操作:

'use strict';

var should = require('should'); 
var assert = require('assert');
var request = require('supertest');  
var winston = require('winston');


describe('Routing', function() {

  describe('asset', function() {
    it('Crear un nuevo activo en la base de datos y retornarlo', function(done) {
      var asset = {
        "description"      : "Computador portatil",
        "maker"            : "Lenovo",
        "module"           : "Y410",
        "serialNumber"     : "123123",
        "bardCode"         : "21212",
        "account"          : 1212,
        "usefulLife"       : 24,
        "downtimeCosts"    : 200,
        "purchasePrice"    : 120000,
        "assetState_id"    : 1,
        "assetCategory_id" : 3,
        "assetSystem_id"   : 3,
        "supplier"         : 1
      };
    request(url)
    .post('/asset/create')
    .send(asset)
    .end(function(err, res) {
          if (err) {
            throw err;
          }
          console.log (res.should.have);
          res.should.have.status(200);
          done();
        });
    });
  });
});

POST 操作如下:

router.post('/create', function(req, res, next) {
  models.asset.create(req.body

  ).then(function(asset) {
    res.send(asset.dataValues)  
  }).catch(function(error) {
    console.log (error);
    res.status(500).send(error);
  });
});

运行测试时,res.should.have.status(200); 行中出现以下错误Uncaught TypeError: undefined is not a function 我想原因是因为我没有明确说明状态响应,所以我将 res 更改为 `res.status(200).send( assets.dataValues) 但不起作用

【问题讨论】:

    标签: javascript node.js mocha.js


    【解决方案1】:

    我认为你应该试试下面的代码:

    should(res).have.property('status', 200);
    

    解决了 res 没有属性“.should”的问题。

    【讨论】:

      【解决方案2】:

      http://chaijs.com/guide/styles/

      var assert = require('chai').assert;
      var should = require('chai').should();
      

      【讨论】:

      • 您好,感谢您的回答,我使用sudo npm install chai --save-dev 安装链并更改了变量,但错误仍然存​​在
      • 您查看我发布的网址了吗?你在用 mocha runner 吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2013-10-18
      • 2016-12-14
      • 2019-09-28
      相关资源
      最近更新 更多