如果它是私有的,我如何模拟path.resolve()?

时间:2015-02-23 22:05:13

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

我有一个私有函数,我试图使用Mocha,Chai和Sinon来模拟path.resolve()

现在,TypeError: Arguments to path.resolve must be strings获得var projectDir = path.resolve(__dirname + "../../../");.。我不知道该如何解决这个问题,因为它是私密的,我无法嘲笑它......并且它没有采取参数函数,所以我无法提供任何东西。

有什么建议吗?

节点脚本:

function constructDestwCallBack(absSrcFile, callback) {
    console.log(path.resolve(__dirname + "../../../"))
    var projectDir = path.resolve(__dirname + "../../../");
    ...

function foo(callback) {
    var destinationFile;
    errorCheckArg(arguments);
    return through2.obj(function(file, enc, next) {
        destinationFile = constructDestwCallBack(absSrcFile, callback);

测试:

describe('gulp-foo', function() {
    var fakeFile, pspy;

    beforeEach(function() {
        mock({
            '/apple/pear/foo.less': mock.file({
            content: 'nothing',
            mtime: new Date(Date.now())
            })
        });
        fakeFile = new File({
            contents: new Buffer('/apple/pear/foo.less')
        });

    });
    afterEach(mock.restore);


    describe('get files', function() {

        it('should do something', function(done) {
            var path = { resolve: function() { return "ssss"} };
            sinon.spy(path, "resolve");


            var bar = function(dest) { return dest};
            stream = foo(bar);
            done();
        });
    });

1 个答案:

答案 0 :(得分:0)

您可以使用rewire访问测试版私有变量下的模块,并模拟被测模块访问的path.resolve函数。