2016-11-18 124 views
0

我正在嘗試使用Jasmine測試REST API調用。任何人都可以請解釋如何處理像Jasmine這樣的異步測試?使用Jasmine測試REST API調用

============================== answer.js ============= =================

var conf = require('../config'); 
 
var req = require('request'); 
 
var fs = require('fs'); 
 

 
function base64_encode(file) { 
 
\t var file = fs.readFileSync(file); 
 
\t return new Buffer(file).toString('base64'); 
 
} 
 

 
var answers = function(){ 
 
\t this.getAnswer = function(id, branchId, locale, question, deviceId){ 
 
\t \t var answer; 
 
\t \t req({ 
 
\t \t \t url : conf.baseUrl + '/api/v1/answers', 
 
\t \t \t method : 'POST', 
 
\t \t  headers: { 
 
\t \t   'content-type': 'text/plain', 
 
\t \t  }, 
 
\t \t \t body : JSON.stringify({ 
 
\t \t \t \t id : id, 
 
\t \t \t \t branchId : branchId, 
 
\t \t \t \t locale : locale, 
 
\t \t \t \t question : base64_encode("./" + question + ".wav"), 
 
\t \t \t \t deviceId : deviceId 
 
\t \t \t }) 
 
\t \t }, function(error, response, body) { 
 
\t \t \t if (error) { 
 
\t \t \t \t console.log(error); 
 
\t \t \t } else { 
 
\t \t \t \t answer = JSON.parse(body).answer; 
 
\t \t \t \t console.log(JSON.parse(body).responseText); 
 
\t \t \t \t fs.writeFile("../answer.wav", answer, 'base64', 
 
\t \t \t \t function(err) { 
 
\t \t \t \t \t if (err) { 
 
\t \t \t \t \t \t return console.log(err); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t console.log("file saved successfully!"); 
 
\t \t \t \t }); 
 
\t \t \t } 
 
\t \t }); 
 
\t \t return answer; 
 
\t } 
 
} 
 

 
module.exports = new answers();

==================== === loan_application_spec.js ======================

var answers_api = require('../requests/answers'); 
 

 
describe("Loan application", function() { 
 
\t it("Ask about loans", function() { 
 
\t \t console.log(answers_api.getAnswer("1234", "HLB_02", "en-US", "input", "robot_01")); 
 
\t }); 
 
});

回答

0

下面是我如何修改它以適應測試。

  • 我改變了答案是一個對象,而不是一個函數。但我仍然 使用new關鍵字來實例爲便於測試
  • 可能是,如果你能重構REQ對象的實現,它 可能會更容易讓你測試

這裏是一個post,幫助我構建這個答案。

var conf = require('../config'); 
    var req = require('request'); 
    var fs = require('fs'); 

    function base64_encode(file) { 
    var file = fs.readFileSync(file); 
    return new Buffer(file).toString('base64'); 
    } 

    var answers = { 
    ans: "", 
    getAnswer : function(id, branchId, locale, question, deviceId){ 
     var answer;  
     req({ 
      url : conf.baseUrl + '/api/v1/answers', 
      method : 'POST', 
      headers: { 
       'content-type': 'text/plain', 
      }, 
      body : JSON.stringify({ 
       id : id, 
       branchId : branchId, 
       locale : locale, 
       question : base64_encode("./" + question + ".wav"), 
       deviceId : deviceId 
      }) 
     }, function(error, response, body) { 
      if (error) { 
       console.log(error); 
      } else { 
       answer = JSON.parse(body).answer; 
       console.log(JSON.parse(body).responseText); 
       fs.writeFile("../answer.wav", answer, 'base64', 
       function(err) { 
        if (err) { 
         return console.log(err); 
        } 
        console.log("file saved successfully!"); 
       }); 
      } 
     }); 
     this.ans = answer; 
     return answer; 
     } 
    } 

    return module.exports = Object.create(answers); 

    //Jasmine test 

    var answers_api = require('../requests/answers'); 

    describe("Loan application", function() { 
     it("Ask about loans", function() { 
     req = jasmine.createSpy().andCallFake(function() { 
      answers_api.ans = true // actually make this answer whatever you expect it to be. 
     }); 
     var testObj = answers_api.getAnswer("1234", "HLB_02", "en-US", "input", "robot_01")); 
     expect(req).toHaveBeenCalled(); 
     expect(answers_api.ans).toBe(true) 
     }); 
    }); 
+0

在this.getAnswer = function和return module.exports = Object.create(answers)中有語法錯誤;線。 另外,我得到了您指定的Jasmine代碼的以下錯誤。 TypeError:jasmine.createSpy(...)。和CallFake不是函數 在Object處。

+0

我對答案做了一些小的更正。請現在試試。 –

+0

感謝您的回覆。但仍然有一個語法錯誤與返回關鍵字與module.exports = Object.create(答案); 另外,當我執行刪除返回時,我得到以下輸出 –