c# - moq callback makes linq throw exception -
i using moq , ef6.
i need query table , if there less 5 dates in table need add more.
so, in code:
public void fillpartylist(observablecollection<party> argpartylist) { argpartylist.clear(); using (ipartytrackerdataaccess localdb = locator.resolve<ipartytrackerdataaccess>()) { initializedates(localdb); var locallist = (from templist in localdb.parties templist.partydate >= datetime.now.date select templist); foreach (party currentparty in locallist) { argpartylist.add(currentparty); } } }
inside "initializedates" query database see how many dates there , add enough make 5. there rules around creating dates [e.g. on fri or sat, etc] not trivial. then, when method comes full list database , add parameter.
my problem comes when try test this. here's test.
public void testinitializedates() { //expected data int callcount = 0; int addparty = 0; int savechanges = 0; observablecollection<party> partylist = new observablecollection<party>(); //mock data iqueryable<party> partydata = new list<party>().asqueryable(); iqueryable<systemsetting> settingsdata = new list<systemsetting>().asqueryable(); //setup mock mock<partytrackerentities> mockcontext = setupdbcontext(); setupapplicationsettings(mockcontext, settingsdata); setuppartydbset(mockcontext, partydata); locator.register<iapplicationsettings, applicationsettings>(); //if keep following line exception when querying "database" parties. mockcontext.setup(m => m.parties.add(it.isany<party>())).callback(() => addparty = callcount++); mockcontext.setup(m => m.savechanges()).callback(() => savechanges = callcount++); //run tests partytrackermodel localmodel = new partytrackermodel(); localmodel.fillpartylist(partylist); assert.areequal(1, callcount); assert.areequal(0, savechanges); } private mock<dbset<party>> setuppartydbset(mock<partytrackerentities> argdbcontext, iqueryable<party> argpartydata) { //mock objects mock<dbset<party>> mockpartyset = new mock<dbset<party>>(); mockpartyset.as<iqueryable<party>>().setup(m => m.provider).returns(argpartydata.provider); mockpartyset.as<iqueryable<party>>().setup(m => m.expression).returns(argpartydata.expression); mockpartyset.as<iqueryable<party>>().setup(m => m.elementtype).returns(argpartydata.elementtype); mockpartyset.as<iqueryable<party>>().setup(m => m.getenumerator()).returns(argpartydata.getenumerator()); argdbcontext.setup(m => m.parties).returns(mockpartyset.object); return mockpartyset; }
edit: got distracted , didn't finish question.
everything works fine until add setup increments counter, linq in initializedates() fails.
how increment counter , leave dbset<> mocking properly? how query "database" , still count calls add? if setup context.parties.add() count calls linq query blows up.
what doing wrong?
edit: adding initializedates(). note: none of has problem until add moq.callback.
private static void initializedates(ipartytrackerdataaccess arglocaldb) { iapplicationsettings localappsettings = locator.resolve<iapplicationsettings>(); //using (partytrackerentities localdb = new partytrackerentities()) { //todo: move code partytrackerdb class. //technically communicate database , //make sure there @ least 5 parties in future. var sqlpartylist = linqlist in arglocaldb.parties linqlist.partydate >= datetime.now.date select linqlist; list<party> partylist; try { partylist = sqlpartylist.orderbydescending(argorder => argorder.partydate).take(5).tolist(); } catch (sqlexception) { //messagebox.show("cannot connect database. please ensure sqlserver running , accessable machine."); throw; } if (partylist.count < 5) { datetime maxpartydate = partylist.count <= 0 ? datetime.now.date.adddays(-1) : partylist.max(argmaxdate => argmaxdate.partydate); //<= 4 because partylist.count zero-based 5 parties. (int shortdaycount = partylist.count; shortdaycount <= 4; shortdaycount++) { int dateoffset; decimal coupledonation; decimal singlemaledonation; decimal singlefemaledonation; decimal sponsoredmaledonation; if (maxpartydate.dayofweek == dayofweek.friday) //if last party in list on friday, need add saturday { //get donation amounts //get saturday donations coupledonation = localappsettings.satcoupledonation; singlemaledonation = localappsettings.satsinglemaledonation; singlefemaledonation = localappsettings.satsinglefemaledonation; sponsoredmaledonation = localappsettings.satsponsoredmaledonation; //add sat dateoffset = 1; } else //otherwise need add friday { //get donation amounts //get friday donations coupledonation = localappsettings.fricoupledonation; singlemaledonation = localappsettings.frisinglemaledonation; singlefemaledonation = localappsettings.frisinglefemaledonation; sponsoredmaledonation = localappsettings.frisponsoredmaledonation; if (maxpartydate.dayofweek == dayofweek.saturday) { dateoffset = 6; } else { dateoffset = 5 - (int)maxpartydate.dayofweek; } } party newparty = new party { partydate = maxpartydate.adddays(dateoffset), coupledonation = coupledonation, singlemaledonation = singlemaledonation, singlefemaledonation = singlefemaledonation, sponsoredmaledonation = sponsoredmaledonation }; arglocaldb.parties.add(newparty); maxpartydate = newparty.partydate; } arglocaldb.savechanges(); } } }
Comments
Post a Comment