我正在使用 mocha + sinon.js + chai。 我有很多测试,有很多 stub 和模拟,所以我的 it() 函数太长。 有什么方法可以在其他文件/配置文件中组织我的 stub 。 我无法将 stub 放入 before() 中,因为每个测试都以不同的方式进行 stub ( stub 相同的对象但具有不同的行为)。
对我来说有什么好主意吗?
tnx:)
最佳答案
您可以使用babel-plugin-inline-json为您的响应加载 json。
例如
安装配置 babel-plugin-inline-json。
//.babelrc:
{
"plugins": [["inline-json", {"matchPattern": "config"}]]
}
在外部文件中定义您的响应,例如:
//res.config.json
{
"foo": "bar"
}
在测试中使用它:
const expect = require('chai').expect;
const sinon = require('sinon');
class SomeObject {
doSomething(){
return null;
}
}
describe('Some tests:', function () {
it.only('With fake response', () => {
const obj = require('./res.config');
const someObject = new SomeObject();
sinon.stub(someObject,'doSomething').returns(obj);
const res = someObject.doSomething();
expect(res).to.haveOwnProperty('foo');
});
});
});
您可以有多个 config.json 文件。
关于node.js - sinon.js - 如何组织我的 stub ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42089512/