【发布时间】:2016-08-09 09:01:25
【问题描述】:
我一直在尝试存根和模拟我的函数以便能够测试我的函数
SetExample.createCandyBox = function(config) {
this.getCandies(config.candyUrl)
.catch(() => {
return {};
})
.then((candies) => {
Advertisements.pushCandyBox(config, candies);
});
};
我想在 config.candyUrl 不正确(404 等)时测试一个场景,例如:
it('should return to an empty array when url is incorrect', sinon.test(function() {
// Fixture is an element I created for testing.
var configStub = SetExample.getCandyConfig(fixture);
var createCandyBoxMock = sinon.mock(config);
createCandyBoxMock.expects('catch');
SetExample. createCandyBox(configStub);
}));
当我这样做时,术语是错误 => 找不到变量:config. 我做错什么了?有人可以帮忙解释一下吗?我是Sinon的新手:(提前谢谢!
【问题讨论】:
-
变量
config从未在您的测试中定义。你为什么期望它在那里,你期望它有什么价值? -
啊抱歉,我用
SetExample替换了config,它适用于我的简单测试函数return "yay!"。但是它仍然没有解决我的测试问题。我只想测试当我在createCandyBox中输入错误的 URL 然后它返回到{}
标签: javascript unit-testing mocha.js sinon chai