错误使用`应该`testingnodeJS应用程序(undefined不是一个函数)
我想使用摩卡testing和REST API,所以我有以下代码来testing创build操作:
'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); }); });
当运行testing时得到以下错误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);
它解决了资源没有“应该”的事实。
请参阅http://chaijs.com/guide/styles/
var assert = require('chai').assert; var should = require('chai').should();