Click here to Skip to main content
13,091,554 members (71,794 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 6 Jan 2014

Mocking Task.Factory.StartNew

, 6 Jan 2014
Rate this:
Please Sign up or sign in to vote.
Task.Factory.StartNew can be very useful for executing a method on another thread where you don't care about the result, but it can also make the host method hard to test. This tip describes how to inject Task.Factory and mock it for testing.


This tip illustrates how the .NET Task.Factory.StartNew(Task) method can be injected as a dependency and mocked for testing. The sample project includes a Test project using nUnit and Rhino Mocks to show the code working, but any unit test and mocking framework can be used to achieve the same result.


On a recent project, I had the requirement to execute calls to an external email subscription service using a separate thread. We did this to improve the user experience. The user didn't need to know if the email trigger was successful or not, so there was no point making the user wait. Also, because we were calling a third party web service, we couldn't guarantee the response time. To solve this, I used the Task.Factory class to start a new task to send the service message to the external web service, but not wait for the response (effectively a fire and forget). The task was responsible for logging any errors.

This all worked well in a proof of concept, but when I started coding the real solution I immediately ran into a problem. I'm a big fan of test driven development, but the call to Task.Factory.StartNew(Action) was making the method hard to test. After trawling the net for a solution, I stumbled across various articles and blogs about using a wrapping interface (some called it a factory, but the final solution I chose is more like a proxy). That solved the problem of injecting the Task.Factory.StartNew(Task) as a dependency. I then also found a solution to use mocks to verify that the correct action is being called. So I can't take credit for the solution below, but I thought it useful to present the two parts together, as they form the total solution (or did in my case).

Creating a Proxy

The first step to making the Task.Factory.StartNew(Task) injectable is to wrap it with our own interface and accompanying implementation. This technique is similar to my previous article on the Proxy Pattern (see Our simple interface exposes a method to start a new task on a separate thread.

public interface ITaskFactory
    void StartTask(Action action);

Next we implement the interface.

public class TaskFactory : ITaskFactory
    public void StartTask(Action action)

Injecting the Proxy

Now that we have an interface for our task factory, we can inject it into a class that needs it. The following class also has a secondary class injected. The secondary class and method (ClassTwo.FireAndForget(int)) will be used as the action for the new task.

public class ClassOne : IClassOne
    private readonly ITaskFactory _taskFactory;
    private readonly IClassTwo _classTwo;
    public ClassOne(ITaskFactory taskFactory, IClassTwo classTwo)
        _taskFactory = taskFactory;
        _classTwo = classTwo;
    public void CallAsync(int number)
        _taskFactory.StartTask(() => _classTwo.FireAndForget(number));
        Console.WriteLine("Response from ClassOne with {0} on thread {1}.", number,

For illustration purposes only, I've added some Console.WriteLine statements that represent the work that the method is doing after the task is started.

Unit Testing

Now I want to unit test ClassOne.CallAsync. I can make use of the ITaskFactory interface to mock the StartTask method. I also want to test that ClassTwo.FireAndForget(int) is the action that will be executed in the new task and that the parameter was passed into correctly. The code below shows the full test class to test these two scenarios.

public class ClassOneTest
    #region SetUp
    private IClassOne _classOne;
    private ITaskFactory _taskFactory;
    private IClassTwo _classTwo;
    public void SetUp()
        // mock the TaskFactory
        _taskFactory = MockRepository.GenerateStub<itaskfactory>();
        // mock the class we want to call asynchronously
        _classTwo = MockRepository.GenerateStub<iclasstwo>();
        // setup system under test
        _classOne  = new ClassOne(_taskFactory, _classTwo);
    #region CallAsync
    public void CallAsyncCallsTaskFactory()
        // system under test
        // assertions
    public void CallAsyncCallsClassTwoByTaskFactory()
        // setup
        Action actionExpected = null;
        _taskFactory.Expect(x => x.StartTask(Arg<action>.Is.Anything))
                    .WhenCalled(y => actionExpected = (Action)y.Arguments[0]);
        // system under test
        // assertions
        _classTwo.AssertWasCalled(e => e.FireAndForget(Arg<int>.Is.Equal(1)));

The SetUp() method creates the mock objects for the dependencies. As mentioned above, we're using Rhino Mocks as our mocking framework. The first test, CallAsyncCallsTaskFactory(), verifies (asserts) that a new task is started when CallAsync is executed. The second test verifies that FireAndForget is the method that will be executed by taking advantage of Rhino Mocks WhenCalled extension. This allows us to retrieve the first argument passed into the TaskFactory mock (which is the action being executed).

The action is the FireAndForget method on the mock we setup for the ClassTwo object, so when we call Invoke() on that action, the mock will allow us to assert that the FireAndForget method is executed and also verify that the parameter passed into it is correct.


  • 7 January 2014: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Craig Jewiss
Software Developer (Senior)
Australia Australia
I've been building line of business applications since 2002, mainly for finance and insurance companies in Australia.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
BORourke7-Jan-14 12:02
memberBORourke7-Jan-14 12:02 
GeneralRe: My vote of 5 Pin
klacs1200030-Mar-15 18:08
memberklacs1200030-Mar-15 18:08 
GeneralMy vote of 5 Pin
Pete O'Hanlon6-Jan-14 21:18
protectorPete O'Hanlon6-Jan-14 21:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 6 Jan 2014
Article Copyright 2014 by Craig Jewiss
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid