public mycontext _db; public override void onactionexecuted(httpactionexecutedcontext actionexecutedcontext) { if (_db == null || !_db.changetracker.haschanges()) { return; } try { _db.savechanges(); } catch { } }
this action filter wep api project. _db context object injected filter per request. point here call savechanges() method once after processing done in service layers. problem how can test filter? how can mimic exception case can happen in controler or service layer , when exception throws savechanges() never called? how can setup case exception occurred in place inside application?
if unhandled exception occurs while executing request exception
property on actionexecutedcontext
contain exception. part of framework, , not need test. in tests can simple set exception
property manually , assert attribute takes correct action.
[fact] public void saves_data_on_failure() { var mockdbcontext = new mock<idbcontext>(); var myattribute = new myattribute(mockdbcontext.object); var executioncontext = new httpactionexecutedcontext { exception = new exception("request failed.") }; myattribute.onactionexecuted(executioncontext); mockdbcontext.verify(d => d.savechanges()); }
you might want consider whether or not want save data types of exception. data might in invalid/unknown state.
Comments
Post a Comment