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 });
});
在this.getAnswer = function和return module.exports = Object.create(answers)中有语法错误;线。 另外,我得到了您指定的Jasmine代码的以下错误。 TypeError:jasmine.createSpy(...)。和CallFake不是函数 在Object处。 –
我对答案做了一些小的更正。请现在试试。 –
感谢您的回复。但仍然有一个语法错误与返回关键字与module.exports = Object.create(答案); 另外,当我执行删除返回时,我得到以下输出 –