2016-12-06 61 views
-2

我有這個測試代碼路由,如何使其功能 ///////////////////////////// //////////////////////////////快速路由摩卡測試

describe("routing",function(){ 
    it("should call function to return list of obj",function(done){ 

     var server = require('../server.js') 
    request(server).get('/listt') 
    .send({'id':3,'name':'name3'}) 
    .end(function(err,res){ 
     console.log("",res.body) 
    if(err){ 
     done(err); 
    }else{ 
     console.log(res.body); 
     var expected =[{'id':1,'name':'name1'},{'id':2,'name':'name2'},{'id':3,'name':'name3'}]; 
     expect(res.body).to.equal (expected) 
     done(); 
     } 
    }); 
    }); 
}); 
+0

你的問題確實含糊不清,你需要仔細閱讀如何測試路線。這是一個很好的鏈接:http://javascriptplayground.com/blog/2014/07/testing-express-routes/ – Baruch

+0

你只使用摩卡或柴? – rule

+0

不只是摩卡 我想問.send({id:3,name:「name3」})這是什麼意思 – Maryam

回答

0

您正在從您的描述中尋找基本測試,正在使用:

摩卡:https://mochajs.org/

柴:http://chaijs.com/

chaiHttp:https://github.com/chaijs/chai-http

然後用這3你能變換成這樣的:

describe("Routing", function() { 
    it("should call function to return list of obj", function(done){ 
    chai.request(server) 
     .post('/listt') 
     .send('Whatever you want to send') 
     .end(function(err, res) { 
     //do whatever verifications you want here 
     done(); 
    }); 
}); 

有噸的方法來測試路線,這只是其中easyest一個習慣(我認爲)。

+0

如何編寫從這個測試路由 – Maryam

+0

你是頭頂上的方式,你試圖建立路由測試,當你不瞭解路由第一!只是先學習路線和他們如何工作,然後擔心測試。 https://codeforgeek.com/2015/05/expressjs-router-tutorial/ – rule