c# - Preferred way to mock/stub a system resource like System.Threading.Mutex -


i have c# class uses mutex control access global shared resource. mutex not assigned unless shared resource created. i'm using partial mock of class isolate testing of methods grab mutex, work, , release (without ever creating shared resource or mutex instance).

i'm curious know if there preferred way of mocking mutex. abstract real calls waitone , releasemutex methods on class , mock those. explicitly assign new mutex instance class' mutex member variable in test (assuming compromise , make setter accessible unit test). wrap mutex in class implements parallel interface mutex methods , inject class under test.

are there better ways of mocking system resources system.threading.mutex? far i've opted abstract real calls methods on class , mock them.

look microsoft fakes framework. has called shims work little differently stubs or mocks divert calls delegate.

the framework create class called shimmutex. in test class write delegates redirect mutex methods use , write code in test along lines of:

using (shimscontext.create()) {     system.fakes.shimmutex.allinstances.waitone = () => { code };     system.fakes.shimmutex.allinstances.releasemutex = () => { code };     //the rest of test } 

note: shims have looked not used yet - answer based on documentation rather experience. documentation pretty thorough though.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -