views:

113

answers:

3

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++;
        }
    }
+3  A: 

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.

Brian Rasmussen
I'm using Moq but not sure how I would mock it. How would you do it in TypeMock?
Schotime
TypeMock is unique in that it modifies the IL, which allows it to mock entities that you could not otherwise mock. I would still recommend the injection approach first, but if that is somehow not possible, you could look at TypeMock.
Brian Rasmussen
A: 

What does your process do? Is there any way you could check that it is doing what it's supposed to do? For example, it might write to a file or a database table. Or it might expose an API (IPC, web-service, etc.) that you could try calling with test data.

From a TDD perspective, it might make make sense to plug in a "mock/test process" that performs some action that you can easily check. (This may require code changes to allow your test code to inject something.) This way, you're only testing your invocation code, and not-necessarily testing an actual business process. You could then have different unit tests to test your business process.

Andy White
It starts a new 'Process'.
Schotime
I mean what does your sub-process do? Is there any way to check that your sub-process is doing what it's supposed to?
Andy White
The sub-process executes a new Process.. eg. runs another program with arguments which does all the work. Later i just check if it has finished.
Schotime
A: 

I'm not familiar with C# (I prefer mine without the hash), but you need some sort of interface to the process (IPC or whatever is the most convenient method) so you can send it test requests and get results back. At the simplest level, you would just send a message to the process and receive the result. Or you could have more granularity and send more specific commands from your test harness. It depends on how you have set up your unit testing environment, more precisely how you send the test commands, how you receive them and how you report the results.

I would personally have a test object inside the process that simply receives, runs & reports the unit test results and have the test code inside that object.

Makis