I am not convinced about the pattern, but I am trying to create a test like this: I want to create the Controller, but have the dependencies available as Frozen parameters to the test.
The test is as follows.
[Theory, AutoNSubstituteData]
public void TestService(
[Frozen] ITestService service,
TestController controller,
string value)
{
controller.Test(value);
service.Received().ProcessValue(Arg.Any<string>());
}
I get this error as the test starts.
System.InvalidOperationExceptionAn exception was thrown
while getting data for theory WebTest.Tests.Controllers.TestControllerRouteTests
.TestService:
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. ---> System.NotImplementedException: The method or operation is not implemented.
at System.Web.HttpContextBase.get_Items()
at System.Web.WebPages.DisplayModeProvider.SetDisplayMode(HttpContextBase context, IDisplayMode displayMode)
I have created the AutoNSubstituteData attribute from this AutoNSubsituteData post. I have attempted to create a fake context to solve the problem.
/// <summary>
/// The auto n substitute data attribute.
/// </summary>
internal class AutoNSubstituteDataAttribute : AutoDataAttribute
{
/// <summary>
/// Initialises a new instance of the <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>()));
}
}