sinon/sandbox.js

Sandboxは、restore/verifyを必要とするfakesを用いて容易に動作する。 もし、fake timers, fake XHR, stub, spyをグローバル変数として使うのであれば、簡単に元に戻せるsandboxを使うべきである。 デフォルトでは、sandboxのspy, stub, mockは関数が実行されたオブジェクトにバインドされる、 なので自分でrestore()したくないのであれば、sinon.spy()の代わりに、this.spy()を使うべきである。

依存関係

None

Name Description
sinon/sandbox.js sinon.sandboxを定義
sinon.js  
sinon/collection.js  
sinon/util/fake_timers.js  
sinon/util/fake_server.js  
sinon/util/fake_server_with_clock.js  

継承

collection.js を継承

非公開メソッド

exposeValue(sandbox, config, key, value)
config.injectIntoまたは、sandbox.argsに、valueを設定する。
valueが未設定の場合は、処理を行わない。
パラメタ:
  • sandbox (sandboxオブジェクト) – sinon.create(sinon.sandbox)で生成されたオブジェクト
  • config (Object) – sinon.defaultConfigと同様のプロパティを持ったオブジェクト
  • key (String) – config.injectIntoのHashキー
  • value (Object or Function or リテラル etc...) – config.injectInto[key]または、sandbox.argsに設定する値。
戻り値:

なし。

prepareSandboxFromConfig(config)
configの設定内容を元に、sandboxオブジェクトを生成。
config.useFakeServerにtypeofの結果==”object”が設定されている場合は、config.useFakeServerをsandbox.serverPrototypeに設定して、sinon.useFakeServer()を呼び出す。
config.useFakeTimersにtypeofの結果==”object”が設定されている場合、sandbox.useFakeTimers.apply(...)を呼び出す。”object”でない場合は、sandbox.useFakeTimers()を呼び出す。
パラメタ:config (Object) – sinon.defaultConfigと同様のプロパティを持ったオブジェクト
戻り値の型:sandboxオブジェクト
戻り値:config.useFakeServer / config.useFakeTimersを反映させたsandbox。

ノート

config.useFakeTimersには[0, “Date”, “setTimeout”]を設定できる。 その際に、sandbox.useFakeTimers.apply(sandbox, config.useFakeTimers)が呼び出される。 この記述は、jQueryでも使用されている形で、sandbox.useFakeTimersの第1引数は0、第2引数は”Date”、第3引数は”setTimeout”となる。

公開メソッド

sinon.sandbox.useFakeTimers()
sandboxインスタンスのclockに、 sinon/util/fake_timers.js のsinon.useFakeTimers()の呼び出し結果を設定する。
また、sandbox.clockをsandbox.fakesに追加する。
戻り値の型:sinon.clockを継承したObject
戻り値:sinon.useFakeTimers()の結果を返す。
sinon.sandbox.useFakeServer()
sandbox.serverPrototypeまたは、:doc:sinon/util/fake_server.js <./util/fake_server> のsinon.fakeServerのcreateメソッドを呼び出し、
結果をsandbox.serverに設定する。
sandbox.serverPrototypeまたは、sinon.fakeServerにcreateメソッドが無い場合は、nullを返す。
また、sandbox.serverをsandbox.fakesに追加する。
戻り値の型:sinon.fakeServerを継承したObject
戻り値:sandbox.serverPrototypeまたは、sinon.fakeServerにcreateの戻り値を返す。
sinon.sandbox.inject(obj)
objに、spy/stub/mockメソッドを追加する。
spy/stub/mockはsandboxインスタンスのspy/stub/mockを呼び出す。
sandboxインスタンスにclockが定義されている場合
objに、sandboxインスタンスのclockを設定する。
sandboxインスタンスにserverが設定されている場合
obj.serverにsandboxインスタンスのserverを、obj.requestsにsandboxインスタンスのserver.requestsを追加する。
パラメタ:obj (Object) – sandboxインスタンスのメソッドが追加されるオブジェクト
戻り値の型:Object
戻り値:引数のobjを返す。objにはspy/stub/mock/clock/server/requestsが追加されている。
sinon.sandbox.create(config)
sandboxインスタンスを生成(sinon.sandboxをプロトタイプに持つ新しいインスタンスを生成)
パラメタ:config (object) – configを指定しなくても問題ない。configはsinon.defaultConfigをデフォルト値にとる事が出来る。
戻り値の型:sandbox object.
sinon.sandbox.useFakeXMLHttpRequest()
sinon.sandbox.useFakeServerを参照している。

プロパティ

sinon.sandbox.serverPrototype
初期状態ではsinon.fakeServerを参照している。
prepareSandboxFromConfig非公開メソッド内でconfig.useFakeServerによって書き換えられる可能性もある。
sinon.sandbox.useFakeServerメソッド内で使用される。