MockHttpRequest.Container throwing UnsupportedException

Just upgraded from 4.5.0 to 4.5.4.

In testing we are now getting an UnsupportedException("Use Resolver") when calling this code (that used to work in 4.5.0):

                var request = new MockHttpRequest
                {
                    Dto = new MyDto()
                };
                request.PathInfo = request.Dto.ToGetUrl();
                request.Container.Register(mockCacheClient);

I can see why I am getting the exception from this commit: https://github.com/ServiceStack/ServiceStack/commit/501ad4c5b5f077956d94c4254d981b8e4e548381

What I wanted to know was: what is the new recommended way to setup an instance to be resolved by the Resolver of the MockHttpRequest during testing? (in my case, setup a ICacheClient just for testing purposes)

Most functionality in ServiceStack requires an initialized AppHost, for unit tests you can use the server-less BasicAppHost, e.g:

using (new BasicAppHost {
    ConfigureContainer = c => {
        //Configure deps...
    }
}.Init())
{
    //...                
}

Understood.
In these tests, we not testing an AppHost. We dont (currently) need to initialize it.
We are testing a bit of code that takes the current IRequest and does stuff with it, that’s why we were using the MockHttpRequest object.

So, now I need to construct a real instance of a IRequest to behave the way we wanted it to, i.e. use a specific ICacheClient instance.
(cant use Moq to create a Mock instance in this test).

Any other options?

Not built-in, but you can always take a local copy of MockHttpRequest and modify it.

Is this OK to do?

    var container = new Container();
    container.Register(cacheClient);
    Service.GlobalResolver = new BasicResolver(container);

    var request = new MockHttpRequest
    {
        Dto = new MyDto()
    };

It works. Just checking for maintainability in future.

You can also just do:

Service.GlobalResolver = container;

thanks, that works fine

1 Like