tags:

views:

53

answers:

2

Given a class like System.Timers.Timer, or ANY managed class (whether user defined, from the .net framework, or some 3rd party library) is there some program I can use to (a) generate an interface based on this class and (b) generate a wrapper for the given class?

for example if I have a

public class Foo
{
   public object MyProperty { get { ... } set { ... } }
   public int SomeMethod(object a) { ... }
}

it will create an interface

interface IFoo
{
   object MyProperty { get; set; }
   int SomeMethod(object a) { ... } 
}

and maybe even a wrapper

class FooWrap
{
   // something for relay constructor here
   ...

   Foo _me;

   public object MyProperty { get { return _me.MyProperty; } set { _me.MyProperty =    value; } }
   public int SomeMethod(object a) { return _me.SomeMethod(); }
}

Obviously there's stuff I haven't thought about like events, generics etc. I want a DWIMNWIS-PSICHTO(-Plus-Stuff-I-Clearly-Haven't-Thought-Of).

I'm aware resharper/vs can be used to extract an interface but I've only been able to use this on my own classes.

Aside: Wow, it is amazing how simply becoming accustomed to a previously 'unacceptable' idea eventually gives it legitimacy. A year ago the idea of having to create interfaces for all objects I want to mock and adopting an injection framework would have seemed like the height of madness. It turns out that while it's not quite death and taxes, it is sparta.

I am aware of and have used typemock. It certainly is the work of elvish wizards. One day when $800 does not seem like quite so much money I intend to buy it.

A: 

I hear good things about rhino mock There is also nmock

Sorry but i dont have much extra information about them. Just read a bit about them in a book.

Charles
A: 

I recommend that you create your own interfaces and implement only what you need. Typemock like approach is evil and should only be used on legacy code until it's refactored right.

Maxwell Troy Milton King
I generally don't know what I need until I start using it. Are you saying every time I call a previously unused method on the class I then go and add that method to the interface? This seems like a major pain.
fostandy
I think it's not too bad and worth it. And the smaller you keep you interfaces the better as well. Otherwise I don't know if you have any other option but Typemock. Especially when you think of static method calls etc. in system libraries. I don't know anyway of mocking statics gracefully (ie not without typemock) but wrapping them.
Maxwell Troy Milton King