views:

41

answers:

2

Hi all,

I'm wondering how can I mock IO/Network classes. For example I have a class that do FTP operations or a class that writes into a file. How can I use mocking to mock and unit test such classes?

I'm a C#/NHibernate/Castle ActiveRecord/Rhino Mocks developer.

+1  A: 

Using Rhino Mocks you can mock only interfaces, abstract classes (virtual methods in general). So abstract your network access code into interfaces and then mock them in order to unit test other classes depending on them in isolation.

You cannot unit test code that needs to connect to an FTP server in isolation. That's no longer an unit test, it's an integration test and mocking cannot help you here.

Darin Dimitrov
A: 

For rhinomocks, you have to define an Interface or abstract class so rhinomocks can create a proxy/mock object for you. Other mock frameworks (e.g. typemock and Justmock, both commercial) are able to mock non abstract classes, static classes, non virtual methods etc.

tobsen