How to mock IO/Network classes?
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#/NH开发者_如何学Cibernate/Castle ActiveRecord/Rhino Mocks developer.
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.
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.
精彩评论