2015-10-15 151 views
1

我使用摩卡要测试和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.status(200 ).send(asset.dataValues)但不起作用

回答

1

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

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

它解决了一个事实,即资源不具有财产“应该”。

+0

它的工作,谢谢 – oriaj

1

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

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

嗨,感谢您的回答,我使用'sudo npm install chai --save-dev'安装链并更改了增值税,但是错误持续存在 – oriaj

+0

您是否检查过我发布的网址?你使用摩卡跑步者吗? – sagie