我正在使用 Karma、Jasmine 和 Sinon 为我的 Angular 应用程序编写单元测试,并在我的代码库上运行 eslint。
我定义了将在 beforeEach Inject
中使用的全局变量来创建 sinon.stub
。 ESLint 一直提示我的全局变量已定义但从未使用过。例如:
'getListStub' is defined but never used no-unused-vars
但在我的代码中它看起来像这样
var listService, getListStub;
beforeEach(inject(function(_listService_) {
listService = _listService_;
getListStub = sinon.stub(listService, 'getList').returns(q.when(listResponse));
}
阻止 ESLint 产生错误的最佳方法是什么?
在这些测试文件的顶部设置 /*eslint no-unused-vars: 0*/
是否最好?
最佳答案
如果您没有在任何地方使用 getListStub
,为什么要将其分配给变量?
JS 闭包和内存管理(特别是保存引用对象)的属性将允许您直接使用 _listService_
,并且不需要缓存 getListStub
。
如果这与 sinon 一起工作正常,您应该能够将您的函数重写为:
beforeEach(inject(function(_listService_) {
sinon.stub(_listService_, 'getList').returns(q.when(listResponse));
}
关于javascript - 诗农和埃斯林特,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34773219/