I have a method using
HostingEnvironment.QueueBackgroundWorkItem
which I wish to unit test some behaviour before this call, however, the test is failing with System.InvalidOperationException : Operation is not valid due to the current state of the object.
I suspect this I need to mock the HostingEnvironment but unaware of how to.
I ended up doing this to keep things simple:
Then to fire a job simply:
A little bit neater than calling
HostingEnvironment.QueueBackgroundWorkItem
regardless of whether there's an ASP.NET AppDomain then catching theInvalidOperationException
is:To resolve this issue I defined an interface
In production code I inject implementation
In test code I inject implementation
I think this is an OK solution since unit tests work and my classes that queue background tasks are decoupled from
HostingEnvironment
.