c# - My Mocked DbContext writes to db in Unit Test using Moq -
i new mocking , using moq. first time trying mock add together functionality. have mocks setup , works read functions, when seek unit test add together method, saves changes db.
how mock add together method?
i think have mock unitofwork
class has savechanges()
method. want know how have setup mock intercept phone call savechanges()
, not save db.
here mocks have far
[setup] public void setup() { addcount = 0; ienumerable<platform> platformlist = new list<platform>(){ new platform() { id = 1, name = "unknown"}, new platform() { id =2, name = "amazon"}, new platform() { id = 3, name = "prime pantry"} }; var platformdata = platformlist.asqueryable(); var mockplatformset = new mock<dbset<platform>>(); mockplatformset.as<iqueryable<platform>>().setup(m => m.provider).returns(platformdata.provider); mockplatformset.as<iqueryable<platform>>().setup(m => m.expression).returns(platformdata.expression); mockplatformset.as<iqueryable<platform>>().setup(m => m.elementtype).returns(platformdata.elementtype); mockplatformset.as<iqueryable<platform>>().setup(m => m.getenumerator()).returns(platformdata.getenumerator()); mockplatformset.setup(m => m.add(it.isany<platform>())).callback(() => addcount++); var mockcontext = new mock<applicationdbcontext>(){ callbase = true }; mockcontext.setup(m => m.platforms).returns(mockplatformset.object); mockcontext.setup(m => m.platforms.add(it.isany<platform>())); mockcontext.setup(m => m.platforms.add(it.isany<platform>())).callback(() => addcount++); unitofwork = new unitofwork(mockcontext.object); platformrepo = new platformrepository(mockcontext.object); controller = new platformcontroller(platformrepo, unitofwork); }
adding unitofwork code
public class unitofwork : iunitofwork { private readonly dbcontext _context; private bool _isdisposed = false; public unitofwork(dbcontext context) { _context = context; } public void savechanges() { _context.savechanges(); } protected virtual void dispose(bool disposing) { if (!_isdisposed) { if (disposing) { _context.dispose(); } } _isdisposed = true; } public void dispose() { dispose(true); gc.suppressfinalize(this); } }
updated question
i trying unit test create
method of platformcontroller
. in method, phone call add
function on repostiory savechanges
function of unitofwork
. want verify platform
object gets added dbset, 'intercept' phone call savechanges()
not write db.
how can this?
when set callbase
value of mockcontext
iunitofwork
false
, seems have solved problem of unit test writing database.
this line of code: mockcontext.as<iunitofwork>().callbase = false;
here code of setup
function
[setup] public void setup() { addcount = 0; ienumerable<platform> platformlist = new list<platform>(){ new platform() { id = 1, name = "unknown"}, new platform() { id =2, name = "amazon"}, new platform() { id = 3, name = "prime pantry"} }; var platformdata = platformlist.asqueryable(); var mockplatformset = new mock<dbset<platform>>(); mockplatformset.as<iqueryable<platform>>().setup(m => m.provider).returns(platformdata.provider); mockplatformset.as<iqueryable<platform>>().setup(m => m.expression).returns(platformdata.expression); mockplatformset.as<iqueryable<platform>>().setup(m => m.elementtype).returns(platformdata.elementtype); mockplatformset.as<iqueryable<platform>>().setup(m => m.getenumerator()).returns(platformdata.getenumerator()); mockplatformset.setup(m => m.add(it.isany<platform>())).callback(() => addcount++); var mockcontext = new mock<applicationdbcontext>(){ callbase = true }; mockcontext.setup(m => m.platforms).returns(mockplatformset.object); mockcontext.setup(m => m.platforms.add(it.isany<platform>())); mockcontext.setup(m => m.platforms.add(it.isany<platform>())).callback(() => addcount++); mockcontext.setup(m => m.set<platform>()).returns(mockplatformset.object); mockcontext.as<iunitofwork>().callbase = false; unitofwork = new unitofwork(mockcontext.object); platformrepo = new platformrepository(mockcontext.object); controller = new platformcontroller(platformrepo, unitofwork); }
c# entity-framework unit-testing moq
No comments:
Post a Comment