2015-02-08 51 views
0

以下测试规范模拟调用将某些内容写入文件系统的模块。它在内部使用fs来做到这一点。我想确保使用正确的参数调用fs.writeFile()。但是,toHaveBeenCalledWith()似乎不适用于泛型函数参数。任何关于如何让我有希望工作的想法?如何使用Jasmine验证模块调用具有正确参数的子模块方法

测试规格:

var fs = { 
    writeFile: function (arg1, arg2, cb){} 
} 

var writeContent = { 
    toFS: function(){ 
     var path = "some/calculated/path"; 
     var content = "some content"; 
     fs.writeFile(path, content, function(){}) 
    } 
} 

describe("writeContent", function() { 

    var writeFileSpy = null; 

    beforeEach(function() { 

     writeFileSpy = jasmine.createSpy('writeFileSpy'); 
     spyOn(fs, 'writeFile').and.callFake(writeFileSpy); 
    }); 

    it("can call spy with callback", function() { 

     writeContent.toFS(); 

     expect(writeFileSpy).toHaveBeenCalledWith("some/calculated/path", "some content", Function); 
    }); 

}); 

结果:

Message: 
    Expected spy writeFileSpy to have been called with [ 'some/calculated/path', 'some content', Function ] but actual calls were [ 'some/calculated/path', 'some content', Function ]. 

回答

1

回答()我自己的问题:-)只需要括在jasmine.any功能,如:

expect(writeFileSpy).toHaveBeenCalledWith("some/calculated/path", "some content", jasmine.any(Function));