很容易清理锡农树桩

Cleaning up sinon stubs easily

本文关键字:很容易      更新时间:2023-12-10

有没有一种方法可以轻松重置所有的sinon spys mock和stub,它们将与mocha的beforeEach块一起干净地工作。

我看到沙箱是一种选择,但我不知道你如何使用沙箱进行

beforeEach ->
  sinon.stub some, 'method'
  sinon.stub some, 'mother'
afterEach ->
  # I want to avoid these lines
  some.method.restore()
  some.other.restore()
it 'should call a some method and not other', ->
  some.method()
  assert.called some.method

Sinon通过使用Sandboxes提供此功能,Sandboxes可以通过以下几种方式使用:

// manually create and restore the sandbox
var sandbox;
beforeEach(function () {
    sandbox = sinon.sandbox.create();
});
afterEach(function () {
    sandbox.restore();
});
it('should restore all mocks stubs and spies between tests', function() {
    sandbox.stub(some, 'method'); // note the use of "sandbox"
}

// wrap your test function in sinon.test()
it("should automatically restore all mocks stubs and spies", sinon.test(function() {
    this.stub(some, 'method'); // note the use of "this"
}));

以前的答案建议使用sandboxes来实现这一点,但根据文档:

自sinon@5.0.0,sinon对象是默认的沙箱。

这意味着清理你的存根/模拟/间谍现在就像一样简单

var sinon = require('sinon');
it('should do my bidding', function() {
    sinon.stub(some, 'method');
}
afterEach(function () {
    sinon.restore();
});

@keithjgrant答案的更新。

v2.0.0版本起,sinon.test方法已移至单独的sinon-test模块。为了让旧的测试通过,你需要在每个测试中配置这个额外的依赖项:

var sinonTest = require('sinon-test');
sinon.test = sinonTest.configureTest(sinon);

或者,您可以不使用sinon-test并使用沙盒:

var sandbox = sinon.sandbox.create();
afterEach(function () {
    sandbox.restore();
});
it('should restore all mocks stubs and spies between tests', function() {
    sandbox.stub(some, 'method'); // note the use of "sandbox"
} 

您可以使用sinon.collection,如sinon库的作者在这篇博客文章(日期为2010年5月)中所示。

sinon.collection api已经改变,使用它的方法如下:

beforeEach(function () {
  fakes = sinon.collection;
});
afterEach(function () {
  fakes.restore();
});
it('should restore all mocks stubs and spies between tests', function() {
  stub = fakes.stub(window, 'someFunction');
}

restore()只恢复存根功能的行为,但不会重置存根的状态。您必须使用sinon.test包装测试并使用this.stub,或者在存根上单独调用reset()

如果你想要一个设置,它会让sinon总是为所有测试重置自己:

helper.js:

import sinon from 'sinon'
var sandbox;
beforeEach(function() {
    this.sinon = sandbox = sinon.sandbox.create();
});
afterEach(function() {
    sandbox.restore();
});

然后,在您的测试中:

it("some test", function() {
    this.sinon.stub(obj, 'hi').returns(null)
})

注意,当使用qunit而不是mocha时,您需要将它们封装在一个模块中,例如

module("module name"
{
    //For QUnit2 use
    beforeEach: function() {
    //For QUnit1 use
    setup: function () {
      fakes = sinon.collection;
    },
    //For QUnit2 use
    afterEach: function() {
    //For QUnit1 use
    teardown: function () {
      fakes.restore();
    }
});
test("should restore all mocks stubs and spies between tests", function() {
      stub = fakes.stub(window, 'someFunction');
    }
);

创建一个沙盒,它将充当所有间谍、存根、mock和fake的黑盒容器。

你所要做的就是在第一个描述块中创建一个沙箱,这样,它就可以在所有测试用例中访问。一旦完成了所有的测试用例,就应该释放原始方法,并使用afterEach钩子中的方法sandbox.restore()清理存根,以便在运行时释放占用的资源afterEach测试用例通过或失败。

这里有一个例子:

 describe('MyController', () => {
    //Creates a new sandbox object
    const sandbox = sinon.createSandbox();
    let myControllerInstance: MyController;
    let loginStub: sinon.SinonStub;
    beforeEach(async () => {
        let config = {key: 'value'};
        myControllerInstance = new MyController(config);
        loginStub = sandbox.stub(ThirdPartyModule, 'login').resolves({success: true});
    });
    describe('MyControllerMethod1', () => {
        it('should run successfully', async () => {
            loginStub.withArgs({username: 'Test', password: 'Test'}).resolves();
            let ret = await myControllerInstance.run();
            expect(ret.status).to.eq('200');
            expect(loginStub.called).to.be.true;
        });
    });
    afterEach(async () => {
        //clean and release the original methods afterEach test case at runtime
        sandbox.restore(); 
    });
});
下面的

将重置所有存根和嵌套的存根。

sinon.reset();

否则你会 NameOfFunctiontionYouWantToReset.resetHistory();

喜欢addingStub.resetHistory();