如何使用Sinon在两个测试用例中存根相同的方法?

时间:2019-02-27 10:37:34

标签: node.js unit-testing mocha sinon sinon-chai

我正在使用Node,Mongoose,Sinon,Mocha。

在DAO层中,我有名为methodA,methodB的方法。在服务层中,我有servMethodA(调用methodA),servMethodB(调用methodB),servMethodC。现在,servMethodC从DAO调用methodA,然后嵌套在其中的对methodB的调用。

在服务层的测试用例中,我已经对methodA和methodB进行了打桩。如何再次将它们存入servMethodC的测试用例中?

这些是我的服务方法。

function findLikeByPostIdAndUserId(postId, userId) {
        return new Promises((resolve, reject) => {
            likeDislikeDao.findLikeByPostIdAndUserId(postId, userId).
                then((data) => {
                    resolve(data);
                })
                .catch((error) => {
                    reject(error);
                });
        });
    }

function findDislikeByPostIdAndUserId(postId, userId) {
    return new Promises((resolve, reject) => {
        likeDislikeDao.findDislikeByPostIdAndUserId(postId, userId).
            then((data) => {
                resolve(data);
            })
            .catch((error) => {
                reject(error);
            });
    });
}

function saveLike(like) {
    console.log(like);
    return new Promises((resolve, reject) => {
        console.log(data);
        likeDislikeDao.findLikeByPostIdAndUserId(like.postId, like.userId).
            then((data) => {

                if (!data) {
                    likeDislikeDao.findDislikeByPostIdAndUserId(like.postId, like.userId).
                        then((dislikeData) => {
                            if (!dislikeData) {
                                likeDislikeDao.saveLike(like).
                                    then((data) => {
                                        resolve(data);
                                    });
                            }
                            else {
                                likeDislikeDao.deleteDislike(dislikeData._id)
                                    .then((data) => {
                                        likeDislikeDao.saveLike(like).
                                            then((data) => {
                                                resolve(data);
                                            });
                                    });
                            }
                        });
                }
                else {
                    likeDislikeDao.deleteLike(data._id)
                        .then((data) => {
                            //likeDislikeDao.saveLike(like).
                            //  then((data) => {
                            //     resolve(data);
                            // });
                            resolve(data);
                        });
                }
            })
            .catch((error) => {
                reject(error);
            });;
    });
}

这是我个人的测试用例。

    describe('saveLike', function () {
    it('should add a like', function () {
        var stub = sinontwo.stub(likeDislikeDao, 'saveLike');
        stub.withArgs(newLike).callsFake(() => {
            return Promise.resolve(newLike);
        });
        var stubtwo = sinontwo.stub(likeDislikeDao, 'saveDislike');
        stubtwo.withArgs(newDislike).callsFake(() => {
            return Promise.resolve(newDislike);
        });
        const stubthree = sinontwo.stub(likeDislikeDao, 'findLikeByPostIdAndUserId');
        stubthree.callsFake(() => {
            return Promise.resolve(like);
        });
        const stubfour = sinontwo.stub(likeDislikeDao, 'findDislikeByPostIdAndUserId');
        stubfour.callsFake(() => {
            return Promise.resolve(dislike);
        });
        likeDislikeService.saveLike(newLike).then(response => {
            console.log('1 -> ');
            console.log(response);
            assert.length(response, 1);
        });
        stub.withArgs(like).callsFake(() => {
            return Promise.reject('');
        });
        stubtwo.withArgs(dislike).callsFake(() => {
            return Promise.reject('');
        });
        likeDislikeService.saveLike(like).then(response => {
            console.log('2 -> ');
            console.log(response);
            assert.lengthOf(response, 1);
        }).then((err) => {
            console.log(err);
            assert.isDefined(err);
        });
    });
});

describe('findLikeByPostIdAndUserId()', function () {
    it('should find likes by post id and user id', function () {

        likeDislikeService.findLikeByPostIdAndUserId(1,2).then(response => {
            assert.length(response, 1);
        });
        likeDislikeService.findLikeByPostIdAndUserId(1,2).then(response => {
            assert.length(response, 1);
        }).catch((err) => {
            //console.log(err);
            assert.isDefined(err);
        });
    })
});

describe('findDislikeByPostIdAndUserId()', function () {
    it('should find dislikes by post id and user id', function () {

        likeDislikeService.findDislikeByPostIdAndUserId(1,2).then(response => {
            assert.length(response, 1);
        });

        likeDislikeService.findDislikeByPostIdAndUserId(1,2).then(response => {
            assert.length(response, 1);
        }).catch((err) => {
            console.log(err);
            assert.isDefined(err);
        });
    })
});

问题是,我没有获得覆盖范围内的查找方法的“拒绝”部分。另外,除了“ then”行之外,saveLike方法没有得到适当覆盖。

0 个答案:

没有答案