javascript - 使用 `should` 测试 nodeJS 应用程序时出错(undefined 不是函数)

标签 javascript node.js mocha.js

我想使用 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);
  });
});

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

最佳答案

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

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

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

关于javascript - 使用 `should` 测试 nodeJS 应用程序时出错(undefined 不是函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33152392/

相关文章:

react-native - 获取 ReferenceError : fetch is not defined when running react native tests in mocha

javascript - 将添加的标签值存储在变量中

javascript - 使用 jQuery/JavaScript 做出答案后删除文本

javascript - 将多个 Backbone View $el 合并到一个 jQuery 对象中

node.js - 我在创建 react 文件时收到此错误

javascript - 如何从 Node.js 调用 Ruby?

javascript - 如何测试从实时网站提取数据的 AJAX 应用程序?

JavaScript:使用页面对象选择下拉列表项进行 Protractor 测试

jquery - 在 Mocha 测试中使用 jQuery,使用 Node 而不是 io.js

javascript - 单元测试中安装组件的差异