[英]Test REST api call using Jasmine
I am trying to test a REST api call using Jasmine. 我正在尝试使用Jasmine测试REST api调用。 Can anyone please explain how to deal with a asynchronous test like this using Jasmine?
谁能解释一下如何使用Jasmine处理这样的异步测试?
==============================answer.js============================== ============================ answer.js ================= =============
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 = function (){ this.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!"); }); } }); return answer; } } module.exports = new answers();
=======================loan_application_spec.js====================== ======================= loan_application_spec.js =======================
var answers_api = require('../requests/answers'); describe("Loan application", function() { it("Ask about loans", function() { console.log(answers_api.getAnswer("1234", "HLB_02", "en-US", "input", "robot_01")); }); });
Here is how I'd modified it a bit to suit the testing. 这是我对它进行一些修改以适合测试的方式。
Here is a post that helped me construct this answer. 这是帮助我构建此答案的帖子 。
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)
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.