Thursday 15 August 2013

xunit.net - creating controller in a test using AutoNSubstituteData xUnit and AutoFixture -



xunit.net - creating controller in a test using AutoNSubstituteData xUnit and AutoFixture -

i not convinced pattern, trying create test this: want create controller, have dependencies available frozen parameters test.

the test follows.

[theory, autonsubstitutedata] public void testservice( [frozen] itestservice service, testcontroller controller, string value) { controller.test(value); service.received().processvalue(arg.any<string>()); }

i error test starts.

system.invalidoperationexceptionan exception thrown while getting info theory webtest.tests.controllers.testcontrollerroutetests .testservice: system.reflection.targetinvocationexception: exception has been thrown target of invocation. ---> system.notimplementedexception: method or operation not implemented. @ system.web.httpcontextbase.get_items() @ system.web.webpages.displaymodeprovider.setdisplaymode(httpcontextbase context, idisplaymode displaymode)

i have created autonsubstitutedata attribute autonsubsitutedata post. have attempted create false context solve problem.

/// <summary> /// auto n substitute info attribute. /// </summary> internal class autonsubstitutedataattribute : autodataattribute { /// <summary> /// initialises new instance of <see cref="autonsubstitutedataattribute"/> class. /// </summary> internal autonsubstitutedataattribute() : base(new fixture() .customize(new autonsubstitutecustomization()) .customize(new httpcontextbasecustomization())) { } } internal class httpcontextbasecustomization : icustomization { public void customize(ifixture fixture) { fixture.customize<viewcontext>(_ => _.omitautoproperties()); fixture.customize<httpcontextbase>(_ => _.fromfactory(() => substitute.for<httpcontextbase>())); } }

the problem here httpcontextbase.items evil, because it's virtual property always throws notimplementedexception.

usually, mock libraries tend not override virtual members default, , suspect case nsubstitute. if correct, 1 alternative configure test double override items property.

another alternative inquire autofixture omit httpcontext property controller, if don't need in test case.

xunit.net autofixture nsubstitute asp.net-mvc-5.2

No comments:

Post a Comment