c# - How to inject a mocked dbcontext in a service layer of a .net application? -
i writing unit tests , having following problem:
i supposed write tests service layer. service layer consists of different kind of services, of implement servicebase
class. main idea of service base expressed in following logic
protected void getcontext(action<datacontext> action) { getcontext(action, datacontextoptions.default); } protected void getcontextautosave(action<datacontext> action) { getcontext(action, datacontextoptions.autosave); } private static void getcontext(action<datacontext> action, datacontextoptions options) { using (var context = new datacontext(options)) { action(context); } }
so whenever interaction db needed, following logic applied in services:
getcontext(datacontext => { perform logic on datacontext; });
the datacontext
class implements base class has constructor setting dbcontext
. how application interacts db. unit testing part have decided go moq
, pleased capabilities. able fake dbcontext
wondering how inject in hierarchy of program can test logic services it. more specific question class above stated {servicebase
,datacontext
,datacontextbase
}, has mocked when call getcontext
method end fake dbcontext
? architecture of logic interaction db, open recommendations. please share thoughts.
Comments
Post a Comment