vinagre vaselina talones para mascarilla los eliminar dolor crema como bicarbonato aspirina aspereza agrietados javascript testing mocha stubbing sinon

javascript - vaselina - Limpiar los talones sinon fácilmente



talones agrietados vaselina (6)

¿Hay alguna manera de restablecer fácilmente todos los simulacros y trozos de sinon spys que funcionarán limpiamente con mocha''s beforeEach blocks?

Veo sandboxing es una opción, pero no veo cómo puedes usar un sandbox para esto

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


Puede usar sinon.collection como se ilustra en this publicación del blog (con fecha de mayo de 2010) por el autor de la biblioteca de sinon.

La API de sinon.collection ha cambiado y una forma de usarla es la siguiente:

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''); }


Si desea una configuración que tendrá sinon siempre se reinicia para todas las pruebas:

en helper.js:

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

Entonces, en tu prueba:

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


Sinon proporciona esta funcionalidad mediante el uso de Sandboxes , que se puede usar de Sandboxes maneras:

// 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" }

o

// 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" }));


Tenga en cuenta que cuando use qunit en lugar de mocha, debe envolverlos en un módulo, por ej.

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''); } );


Una actualización de la respuesta @keithjgrant.

A partir de la versión v.0.0.0 , el método sinon.test se ha movido a un sinon-test separado sinon-test . Para que las viejas pruebas pasen, debes configurar esta dependencia adicional en cada prueba:

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

Alternativamente, puede hacerlo sin sinon-test y usar sandboxes :

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" }


restore() simplemente restaura el comportamiento de la funcionalidad trobada, pero no restablece el estado de los stubs. Tendrás que ajustar tus pruebas con sinon.test y usar this.stub o llamar de manera individual a reset() en los stubs