Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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++;
        }
    }
like image 900
Schotime Avatar asked Jun 02 '09 07:06

Schotime


1 Answers

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.

like image 190
Brian Rasmussen Avatar answered Oct 12 '22 09:10

Brian Rasmussen