.net core - How to unit test ActionFilterAttribute -
i'm looking test actionfilterattribute in .net core 2.0 api project , wondering best way go it. note, i'm not trying test through controller action, merely test actionfilterattribute itself.
how might go testing this:
public class validatemodelattribute : actionfilterattribute { public override void onactionexecuting(actionexecutingcontext context) { if (!context.modelstate.isvalid) { context.result = new badrequestobjectresult(context.modelstate); } } }
create instance of context pass filter , assert expected behavior
for example
[testclass] public class validatemodelattributetest { [testmethod] public void invalid_modelstate_should_return_badrequestobjectresult() { //arrange var httpcontext = new defaulthttpcontext(); var context = new actionexecutingcontext( new actioncontext { httpcontext = httpcontext, routedata = new routedata(), actiondescriptor = new actiondescriptor(), }, new list<ifiltermetadata>(), new dictionary<string, object>(), new mock<controller>().object); var sut = new validatemodelattribute(); //act sut.onactionexecuting(context); //assert context.result.should().notbenull() .and.beoftype<badrequestobjectresult>(); } }
Comments
Post a Comment