Threadpool Mocking
Threadpool mocking is one of the advanced features supported in Telerik® JustMock. It allows you to access mocked objects inside another thread and work with them as expected.
This feature is available only in the commercial version of Telerik JustMock. Refer to this topic to learn more about the differences between the commercial and free versions.
Mocking Inside a Thread
We will use the following sample code to illustrate how you can do this:
Here the WaitLatch
class represents the thread we are going to execute. Inside the thread we will access an instance of the Mockable
class. Let's see a complete example:
We use Mock.Arrange
to specify that the IsMocked
property should return true. Then we create another thread using ThreadPool.QueueUserWorkItem
and access the mocked object. Finally, we assert that the expected value is returned.
Note
Static member mocking and future mocking does not work across threads by default. To make a static or future arrangement valid across all threads, add the .OnAllThreads() clause to the arrangement, e.g.
Mock.Arrange(() => DateTime.Now).Returns(new DateTime()).OnAllThreads();