简体   繁体   中英

How to write a unit test for a function with Promise

I am a complete beginner with Node.JS and Mocha and was tasked to write unit test's for a group project. My problem is that i do not even know where to start since the return value is a promise. Watching a lot of guides i have learned how to check return values for common functions but it wouldn't help me with a real world example. If any expierienced developer could help me with a guide and code example specific to the function i listed i could hack and understand it and apply to other functions as well. Here is a code example that get's statistics from a CSV File

function getStatistics() {
    return new Promise((resolve, reject)=>{
        try {
            let readStatistics = [];
            const statisticsReadStream = fs.createReadStream(statisticsFileName);
            csv.fromStream(statisticsReadStream, {headers: true})
                .transform(function (data) {
                    data.avgDuration = parseFloat(data.avgDuration);
                    data.avgPassed = parseFloat(data.avgPassed);
                    data.avgReachedPoints = parseFloat(data.avgReachedPoints);
                    data.minReachedPoints = parseInt(data.minReachedPoints);
                    data.maxReachedPoints = parseInt(data.maxReachedPoints);
                    return data;
                })
                .on("data", function (data) {
                    readStatistics.push(data);
                })
                .on("end", function () {
                    resolve(readStatistics);
                    statisticsReadStream.close();
                });

        }catch(err){
            reject();
        }
    });

}

In mocha, you can return the promise from the test function to indicate that the test ( it ) has completed.

describe('My Test', function () {
  it('should do something cool', function () {
    return getStatistics().then(readStatistics => {
      // Assert whatever you want here
    });
  });
});

Any error that gets thrown from your getStatistics function or any assertion error will cause the test to fail.

If you are specifically looking to see if something throws an error, you can catch the error ( reject() ) in a test too.

describe('My Test', function () {
  it('should do something cool', function () {
    return getStatistics().catch(error => {
      // Assert whatever you want here about the error
    });
  });
});

https://mochajs.org/#asynchronous-code

Here is the code:

`describe('Statistic', function(){
  it('should transform data into strings', function () {
    return statGet().then(readStatistics => {
      let maybe = statGet();
      var csv = maybe.csv;

      let dat = function (data) {
        data.avgDuration = "1,2";
        data.avgPassed = "2,3";
        data.avgReachedPoints ="3,4";
        data.minReachedPoints = "4";
        data.maxReachedPoints = "5";
        return data;
      }
      assert.typeOf(csv.transform(dat, 'string'));
    });
  });
});`

on the other hand, i have liuttle idea of what i shold be testing in the first place. I feel hopelessly lost. I want to go back to hello world =(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM