2012-07-19 14 views
107

Czy istnieje sposób, aby łatwo zresetować wszystkie mocks i stubs spon sinon, które będą działać czysto z mokrych przed każdym bloków.Czyszczenie kodów dystansowych sinon łatwo

widzę Sandboxing jest opcja, ale nie widzę w jaki sposób można korzystać z piaskownicy do tego

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 

Odpowiedz

246

Sinon zapewnia tej funkcjonalności poprzez wykorzystanie Sandboxes, który może być użyty kilka sposobów:

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

lub

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

Jeśli czytam http: // sinonjs .org/docs/# sinon-test poprawnie w twoim przykładzie 'sinon.test' powinieneś użyć' this.stub (some, 'method'); ' – EvdB

+0

@EvdB Masz rację. Naprawiony. Myślę, że * używanie 'sinon.stub()' również działa, ale lepiej jest grać bezpiecznie i trzymać się tego, jak jest udokumentowane. – keithjgrant

+1

Wydaje się, że jest to obowiązkowe: "Jeśli nie chcesz ręcznie" restore() ", musisz użyć' this.spy() 'zamiast' sinon.spy() '(i' stub', 'mock ')." –

9

można użyć sinon.collection jak pokazano na this wpis na blogu (z maja 2010 r.) Autorstwa autora biblioteki sinon.

sinon.collection api uległa zmianie i sposób go używać jest następujący:

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

Należy pamiętać, że podczas korzystania qunit zamiast mokka, należy owinąć je w module, na przykład

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

qunit 2 przełącza się na 'beforeEach' i' afterEach'. Metody 'setup' i' upardown' będą przestarzałe. –

2

restore() prostu przywraca zachowanie zgaszone funkcjonalności, ale nie resetuje stan odcinki. Musisz albo owinąć testy z sinon.test i używać this.stub lub indywidualnie zadzwonić reset() na odcinki

4

Jeśli chcesz konfigurację, która będzie miała sinon zawsze zresetować się do wszystkich testów:

w helper.js :

import sinon from 'sinon' 

var sandbox; 

beforeEach(function() { 
    this.sinon = sandbox = sinon.sandbox.create(); 
}); 

afterEach(function() { 
    sandbox.restore(); 
}); 

Następnie w teście:

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

Aktualizacja odpowiedzi @keithjgrant.

Od wersji v2.0.0 r metoda sinon.test została przeniesiona do a separate sinon-test module. Aby stare testy przechodzą trzeba skonfigurować tę dodatkową zależność w każdym teście:

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

Alternatywnie, możesz zrobić bez sinon-test i używać 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" 
} 
+1

Albo możesz po prostu użyć pakietu testowego sinon i kontynuować kod jak wcześniej :-D – oligofren