Search code examples
c#unit-testingtdd

How do I unit test code that creates a new Process?


How can I test the following method?

It is a method on a concrete class implementation of an interface.

I have wrapped the Process class with an interface that only exposes the methods and properties I need. The ProcessWrapper class is the concrete implementation of this interface.

    public void Initiate(IEnumerable<Cow> cows)
    {
        foreach (Cow c in cows)
        {
            c.Process = new ProcessWrapper(c);
            c.Process.Start();
            count++;
        }
    }

Solution

  • There are two ways to get around this. The first is to use dependency injection. You could inject a factory and have Initiate() call the create method to get the kind of ProcessWrapper you need for your test.

    The other solution is to use a mocking framework such as TypeMock, that will let you work around this. TypeMock basically allows you to mock anything, so you could use it to provide a mock object instead of the actual ProcessWrapper instances.