承诺使用Mocha返回deferred.promise测试

时间:2018-03-21 23:53:18

标签: node.js mocha

Bot Info

  • SDK平台:Node.js

问题描述

代码示例

resolveMeetingRoomFreeTime: function (session, args) {
    return new Promise(function (resolve, reject) {
        kgService.getFreeMeetingRooms(session.conversationData.floorEntityList,
            session.conversationData.roomEntityList,
            session.conversationData.meetingStartTimestamp,
            session.conversationData.meetingEndTimestamp,
            session.conversationData.meetingStartTime,
            session.conversationData.meetingEndTime,
            session.conversationData.meetingDuration)
            .then(function (roomReservationList) {
                **let returnResults = roomReservationList;
                session.conversationData.meetingRoomAvailabilityList = roomReservationList;
                setTimeout(function () { resolve(returnResults); }, 1000);**
            })
            .catch(function (error) {
                console.log("There is error from getFreeMeetingRooms() "+error);
            });
    }
    )
}

Method getFreeMeetingRooms() looks like as follows. 

    getFreeMeetingRooms: function (nearestFloorEntity, roomEntityList, startEpoch, endEpoch, startHour, endHour, meetingDuration) {
        let deferred = Q.defer();
        let query = function (resolve, reject) {

            graphDB.cypher({
                query: cypherQuery,
                params: {
                    nearestFloorEntity, roomEntityList, startEpoch, endEpoch, startHour, endHour, meetingDuration
                }

            }, function (err, results) {
                return deferred.resolve(results);
            });
            query();
            return deferred.promise;

        }


My test case using Mocha and Sion Stub:

it('Should return default value for resolveMeetingRoomFreeTime()', function () {
    let args = {};
    let session = {
        conversationData: {
            roomEntityList: [{ id: "ABC", name: "ABC", capacity: 8}],
            floorEntityList: "9",
            meetingStartTimestamp: 1521659679948,
            meetingEndTimestamp: 1521781200000,
            meetingStartTime: 1514815200000,
            meetingEndTime: 1514847600000,
            meetingDuration: 30
        }
    };

    //stubbing call for getFreeMeetingRooms()
    var stub = sinon.stub(calendarNeo4jService, "getFreeMeetingRooms")
   .withArgs(session.conversationData.floorEntityList,
        session.conversationData.roomEntityList,
        session.conversationData.meetingStartTimestamp,
        session.conversationData.meetingEndTimestamp,
        session.conversationData.meetingStartTime,
        session.conversationData.meetingEndTime,
        session.conversationData.meetingDuration);

    var error= calendarEntityResolver.resolveMeetingRoomFreeTime(session, args)
    error.then(function name(params) {
    })
    .then(function (error) { 
     });
});

问题:

  1. 如果我需要从方法getFreeMeetingRooms()获取DB调用graphDB.cypher,则不确定使用存根是否正确。
  2. 如何测试resolveMeetingRoomFreeTime()中突出显示的代码块。
  3. 如果存根kgService.getFreeMeetingRooms是正确的,那么测试catch块的方式如何。

1 个答案:

答案 0 :(得分:0)

我认为您正在询问如何使用mocha测试异步功能。如果这是真的,您只需要在it函数内返回您的承诺。在标记成功之前,Mocha将等待承诺解决或拒绝,这也将允许then函数中的断言。