2015-10-15 110 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

相關問題