我有一个这样的模块“sitecollection”:
var site = require('./site'); // <- this should be stubbed
var sitesCollection = function(spec) {
var that = {};
that.sites = {};
that.findOrCreateById = function(id) {
if (typeof(that.sites[id]) == "undefined") {
that.sites[id] = site({id: id}); // <- its used here
}
return that.sites[id];
};
return that;
};
module.exports = sitesCollection;
所以在sitescollection 中,site 是一个不被导出的模块。但在代码内部,我使用它。现在我正在为#findOrCreateById() 编写茉莉花规格。
我想指定我的 findOrCreateBy() 函数。但我想存根 site() 函数,因为规范应该独立于实现。我必须在哪里创建 spyed 方法?
var sitescollection = require('../../lib/sitescollection');
describe("#findOrCreateById", function() {
it("should return the site", function() {
var sites = sitescollection();
mysite = { id: "bla" };
// Here i want to stub the site() method inside the sitescollection module.
// spyOn(???,"site").andRetur(mysite);
expect(sites.findOrCreateById(mysite.id)).toEqual(mysite);
});
});