2016-11-18 191 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(答案); 另外,当我执行删除返回时,我得到以下输出 –