Search code examples
asp.net-mvcunit-testingservicestack

Unit testing functionality that uses the RazorFormat plugin


We have an endpoint that uses RazorFormat to render some html and we are wanting to unit test it.

When we construct a RazorFormat the format is missing the ViewEngine which is of type IRazorViewEngine

I have tried extending the plugin to allow setting the ViewEngine by fetching IRazorViewEngine but it still runs into an issue with ITempDataProvider being null.

Is there a way to instantiate the RazorFormat class while passing in the IRazorViewEngine and any requirements it has without standing up an apphost?

If it does require an app host , what is the simplest way to end up with a functional RazorFormat class ?


Solution

  • IRazorViewEngine is registered by MVC, so requires an App configured with MVC. It's not really unit testable, you'd need at least an AppHost with MVC Services registered, e.g:

    public override void Configure(IServiceCollection services)
    {
        services.AddMvc();
    }
    

    We have an Integration Test that uses Razor Format in ServiceStack.Server.Tests/AppHost.cs which has Razor Integration tests in StatelessAuthRazorTests.cs