c# - Can I mock object which is impossible to instantiate? -
i using com library in c# bound particular hardware , doesn't work without it. on development/testing computer don't have hardware. method using library looks this:
using hwsysmanagerlib; bool processbias(hwsysmanager systemmanager, string hwpath) { int handle = systemmanager.openconfiguration(hwpath); ... // magic goes here // return result }
the question is, can mock hwsysmanager
test method , how? there few methods in hwsysmanager
, wouldn't problem simulate functionality test. tiny example great on how mock it, if it's possible @ all.
you can use adapter pattern here.
create interface named ihwsysmanager
public interface ihwsysmanager { int openconfiguration(string hwpath); }
the real implementation class delegates work library:
public class hwsysmanagerimpl : ihwsysmanager { private hwsysmanager _hwsysmanager; //initialize constructor public int openconfiguration(string hwpath) { return _hwsysmanager.openconfiguration(hwpath); } }
use interface in code this:
bool processbias(ihwsysmanager systemmanager, string hwpath) { int handle = systemmanager.openconfiguration(hwpath); ... // magic goes here // return result }
now can mock ihwsysmanager
interface mock frameworks or can create stub class yourself.
Comments
Post a Comment