如何使用jasmine-node测试函数,jasmine-node在内部调用一个返回promise的函数?

时间:2021-02-06 14:29:17

I am just trying out jasmine-node. I need some help with promise resolution. I have simple js file

我只是在尝试茉莉花节点。诺言解决方案需要一些帮助。我有简单的js文件

//dataService.js

var Q = require('q');
console.info("Q is "+Q);
exports.test = function() {
    console.warn("Will call promise now");
    this.getQuestions().then(function() {
        console.log("Test..");
    });
};

exports.getQuestions = function() {

    var deferred = Q.defer();
    for(i=0; i<=10; i++) {
        if(i===10) {
            deferred.resolve(i);
        }
    }
    return deferred.promise;
    // return {
    //  'Question1': 'What is your name'
    // }
}

//end of dataService.js



And the test is

// testspec.js


var assert = require("assert");
var q = require('q');
var testFile = require('../routes/dataService');
var fs = require('fs');


  describe('#indexOf()', function(done){
    it('should return -1 when the value is not present', function(done){
        console.log("Teststststst" + fs);
      assert.equal(-1, [1,2,3].indexOf(5));
      assert.equal(-1, [1,2,3].indexOf(0));
      spyOn(testFile, 'getQuestions').andCallFake(function() {
            console.warn("Spy Called********************");
            var deferred = q.defer();
            deferred.resolve(1);
            console.info("passing 1****");  
            //done(1);
            return deferred.promise;
      });
      spyOn(console, 'log');
      testFile.test();
      console.info("Testststststsinggggggggg");
      expect(console.log).toHaveBeenCalledWith("Test..");
      console.info("Done*****************");
    })
  });

//end of test file

//测试文件的结尾

Now as you can see I am calling testFile.test() function which is nothing but the test function in dataService.js. This function calls the getQuestions() in the dataService.js (same file), which returns a promise. I have mocked the getQuestions() function in my test, it is getting called and is resolving the promise, but my test() success method is not getting called, so my test is failing.

现在你可以看到我正在调用testFile.test()函数,它只是dataService.js中的测试函数。此函数调用dataService.js(同一文件)中的getQuestions(),该函数返回一个promise。我在我的测试中模拟了getQuestions()函数,它被调用并且正在解析promise,但是我的test()成功方法没有被调用,所以我的测试失败了。

2 个解决方案

#1


2  

Your getQuestions method never resolves the promise.

您的getQuestions方法永远无法解析承诺。

You have a loop running from 0 to 9 but you only resolve it if i === 10.

你有一个从0到9的循环,但你只有在i === 10时解决它。

Change:

for(i=0; i<10; i++) {
    if(i===10) {
        deferred.resolve(i);
    }
}

To:

deferred.resolve(10);

As a general tip methods that call functions that return promises should return promises themselves so you can easly test them and hook on their completion. For this reason I'd make .test return a promise (rather than just call it)

作为返回promises函数的一般提示方法应该返回promises本身,这样你就可以轻松地测试它们并挂钩它们的完成。出于这个原因,我会让.test返回一个承诺(而不仅仅是调用它)

#2


0  

I was able to run the test by returning a promise from the test() function.


//dataService.js

var Q = require('q');
console.info("Q is "+Q);
exports.test = function() {
    console.warn("Will call promise now");
    return this.getQuestions().then(function() {
        console.log("Test..");
        return 'success';
    });

};

exports.getQuestions = function() {

    var deferred = Q.defer();
    for(i=0; i<10; i++) {
        if(i===3) {
            deferred.resolve(i);
        }
    }
    return deferred.promise;
    // return {
    //  'Question1': 'What is your name'
    // }
}

//end of dataService.js



//dataServicespec.js

var assert = require("assert");
var q = require('q');
var testFile = require('../routes/dataService');//include the dataService.js
var fs = require('fs');


describe('Tests', function(done) {
  it('should run the test properly', function(done) {
    console.log("Teststststst" + fs);
    var flag = false;
    var deferred;

    spyOn(testFile, 'getQuestions').andCallFake(function() {
      console.warn("Spy Called********************");
      deferred = q.defer();
      console.info("passing 1****");
      deferred.resolve(1);
      return deferred.promise;
    });

    spyOn(console, 'log');

    runs(function() {
      var p = testFile.test();
      p.then(function() {
        flag = true;
      });
    });

    waitsFor(function() {
      if (flag === true)
        return true;
    });

    runs(function() {
      console.info("Testststststsinggggggggg");
      expect(console.log).toHaveBeenCalledWith("Test..");
      console.info("Done*****************");
      done();
    });



  })
});

//end of dataServicespec.js


Thanx @Benjamin on you suggestion for returning a promise from test.

#1


2  

Your getQuestions method never resolves the promise.

您的getQuestions方法永远无法解析承诺。

You have a loop running from 0 to 9 but you only resolve it if i === 10.

你有一个从0到9的循环,但你只有在i === 10时解决它。

Change:

for(i=0; i<10; i++) {
    if(i===10) {
        deferred.resolve(i);
    }
}

To:

deferred.resolve(10);

As a general tip methods that call functions that return promises should return promises themselves so you can easly test them and hook on their completion. For this reason I'd make .test return a promise (rather than just call it)

作为返回promises函数的一般提示方法应该返回promises本身,这样你就可以轻松地测试它们并挂钩它们的完成。出于这个原因,我会让.test返回一个承诺(而不仅仅是调用它)

#2


0  

I was able to run the test by returning a promise from the test() function.


//dataService.js

var Q = require('q');
console.info("Q is "+Q);
exports.test = function() {
    console.warn("Will call promise now");
    return this.getQuestions().then(function() {
        console.log("Test..");
        return 'success';
    });

};

exports.getQuestions = function() {

    var deferred = Q.defer();
    for(i=0; i<10; i++) {
        if(i===3) {
            deferred.resolve(i);
        }
    }
    return deferred.promise;
    // return {
    //  'Question1': 'What is your name'
    // }
}

//end of dataService.js



//dataServicespec.js

var assert = require("assert");
var q = require('q');
var testFile = require('../routes/dataService');//include the dataService.js
var fs = require('fs');


describe('Tests', function(done) {
  it('should run the test properly', function(done) {
    console.log("Teststststst" + fs);
    var flag = false;
    var deferred;

    spyOn(testFile, 'getQuestions').andCallFake(function() {
      console.warn("Spy Called********************");
      deferred = q.defer();
      console.info("passing 1****");
      deferred.resolve(1);
      return deferred.promise;
    });

    spyOn(console, 'log');

    runs(function() {
      var p = testFile.test();
      p.then(function() {
        flag = true;
      });
    });

    waitsFor(function() {
      if (flag === true)
        return true;
    });

    runs(function() {
      console.info("Testststststsinggggggggg");
      expect(console.log).toHaveBeenCalledWith("Test..");
      console.info("Done*****************");
      done();
    });



  })
});

//end of dataServicespec.js


Thanx @Benjamin on you suggestion for returning a promise from test.