如何测试' catch '和' then '与sinon js
How to test `catch` and `then` with sinon js?
我一直在尝试存根和模拟我的函数,以便能够测试我的函数
SetExample.createCandyBox = function(config) {
this.getCandies(config.candyUrl)
.catch(() => {
return {};
})
.then((candies) => {
Advertisements.pushCandyBox(config, candies);
});
};
我想测试一个场景,当配置。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:(提前谢谢!
您可以使用sinon.stub()
来存根this.getCandies
及其解析/拒绝值。和存根Advertisements.pushCandyBox
,然后做一个断言来检查它是否被调用。
。
SetExample.js
:
const Advertisements = require('./Advertisements');
function SetExample() {}
SetExample.getCandies = async function (url) {
return 'your real getCandies implementation';
};
SetExample.createCandyBox = function (config) {
return this.getCandies(config.candyUrl)
.catch(() => {
return {};
})
.then((candies) => {
Advertisements.pushCandyBox(config, candies);
});
};
module.exports = SetExample;
Advertisements.js
:
function Advertisements() {}
Advertisements.pushCandyBox = function (config, candies) {
return 'your real pushCandyBox implementation';
};
module.exports = Advertisements;
SetExample.test.js
const SetExample = require('./SetExample');
const Advertisements = require('./Advertisements');
const sinon = require('sinon');
describe('38846337', () => {
describe('#createCandyBox', () => {
afterEach(() => {
sinon.restore();
});
it('should handle error', async () => {
const err = new Error('timeout');
sinon.stub(SetExample, 'getCandies').withArgs('wrong url').rejects(err);
sinon.stub(Advertisements, 'pushCandyBox');
await SetExample.createCandyBox({ candyUrl: 'wrong url' });
sinon.assert.calledWithExactly(SetExample.getCandies, 'wrong url');
sinon.assert.calledWithExactly(Advertisements.pushCandyBox, { candyUrl: 'wrong url' }, {});
});
});
});
单元测试结果:
38846337
#createCandyBox
✓ should handle error
1 passing (7ms)
-------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files | 81.82 | 100 | 42.86 | 81.82 |
Advertisements.js | 66.67 | 100 | 0 | 66.67 | 4
SetExample.js | 87.5 | 100 | 60 | 87.5 | 6
-------------------|---------|----------|---------|---------|-------------------
相关文章:
- 未调用 Sinon 存根
- Javascript:If-then语句在函数中不起作用
- angularjs无法读取未定义的属性then
- node.js测试事件是否是在不使用超时的情况下使用sinon.js发出的
- Promise函数在.then之后未运行函数化
- 为什么$.when().pipe().then()有效,而$.when
- Sinon Spy不使用Javascript调用或应用程序
- 如何在异步函数中正确使用Promise.all()和then()
- $resource上带有.then()函数的角度自定义方法导致错误
- Stubbing Backbone类构造函数与Jasmin和Sinon
- 什么'与Jasmine相比,使用Sinon.js的优势是什么;内置于间谍
- 是否可以使用Sinon.JS检查函数参数绑定是否正确
- Sinon窥探功能表达
- 可以链接 '.then() 的返回响应,顺序与 '.then()' 的顺序不同
- AngularJS因果报应测试规范承诺未解析,且未调用.then()
- 在javascript promise中正确使用then
- Sinon Mocha节点错误处理
- 将数据从promise then方法传递到对象方法
- Sinon测试ember.js .then方法
- 如何测试' catch '和' then '与sinon js