c# - Using Autofac and Moqs with Delegate Factories -
i trying unit test class uses factory injection. have class instantiates 2 copies of same object (with different config) control hardware. i'm trying test behaviour of classes while simulating hardware calls.
i've injected set of factory delegates constructors class can instantiate hardware classes required. can't work out how control or create factory methods within autofac.extras.moq package. seems functionality isn't supported in package.
i'm looking equivalent call :
mock.provide<ihwcontroller>(//created factory delegate)
i want create specific mock object behaviour, based on parameters used instantiate hwcontroller. i'm trying possible?
class systemundertest { systemundertest(ia a, ib b, ic c, /** 15 other things **/ func<func<uri, ihwcontroller>, hwtype, ihwmanager> hwmanagerfactory, func<uri, ihwcontroller> hwcontrollerfactory) { } } class hwmanager() { public func<hwtype, func<uri, hwcontroller>, hwmanager> factory; public hwmanager(hwtype type, func<uri, hwcontroller> controlfactory) { //constructor } }
the code i'm unit testing creates managers of controllers. controller hardware layer, i'm testing complex (coupled) behaviour inside manager. therefore, i'm trying work out how mock func<uri, hwcontroller> controlfactory
returns setup mock objects can probe behaviour of manager.
my system under test creates concrete instantiation of hwmanager. realise in perfect scenario test hwmanager , sut separately, i'm testing integration of 2 components.
i'd configure autofac control delegate factory. if isn't possible, can manually setup sut, don't value autofac helper.
i create func returns mocked instance e.g.
var controller = mock.provide<ihwcontroller>(); var manager = new hwmanager(something, (uri) => controller);
if factory expressing "given uri provide controller", lamda in example above satisfies statement.
as aside, should express factories using interfaces, not concrete classes. makes lot harder unit test when factories producing concrete classes instead of interfaces (which can mocked) e.g.
// ihwcontroller product of factory instead of hwcontroller public hwmanager(hwtype type, func<uri, ihwcontroller> controlfactory)
Comments
Post a Comment