views:

516

answers:

2

I'm trying to test some classes I've made using mocks, but I've seen all free mocking frameworks in c# aren't able to mock non-virtual methods (if it is not in an interface).

But, there's TypeMock who can do this, so it is possible. Can anyone show how to do it? I may even try to contribute to an open source framework if I can get this done.

Thanks in advance

+4  A: 

According to the website it uses AOP to redirect calls:

Typemock Isolator uses aspect-oriented technology to redirect calls from the real code. This enables developers to define the behavior of the external component required for a tested scenario. For example, you can simulate that the disk is full when writing to a database by instructing Typemock Isolator to throw an OutOfDiskSpaceException when writing to the database. This is a scenario that will be nearly impossible to test without Typemock Isolator. The developer defines the behavior in the actual unit test and Typemock Isolator automatically isolates all required components.

This technique requires you to set up a special environment before you can run the test or use a VS plugin.

Some people think that the ability to mock everything makes Typemock to powerful, since you don't have to think so much about good design. A quote from Ayende:

The main weakness of Type Mock is its power, it allow me to take shortcuts that I don't want to take, I want to get a system with low coupling and high cohesion.

But of course since he is the creater of Rhino Mock he is not objective :)

I would think you are in for a great deal of work if you want to create a mocking framework with this functionality, so I would recommend to either purchase Typemock, or learn to code without it :)

BengtBe
You do need to set up a special environment before running TypeMock (there's a VS plugin for this as well), and I agree about TypeMock having "more than enough rope to hang yourself with"
Blair Conrad
Thanks Blair, updated the post to reflect your comment!
BengtBe
+4  A: 

Samuel,

I am from Typemock, and I won't address the "Too Powerful" comments (although it's beyond me why people wouldn't want to use the best tool for the job).

Here's how Typemock Isolator works. Have you ever used a performance profiler? Isolator is a profiler. It hooks into the CLR and within the test run time, it changes methods. When a method gets JITted, it changes it, so when the method runs, before executing the original code, it asks: should I run it as originally intended, and if not, what should I do? And because of this specific technology, it can mock any .Net method and technology. That's it.

When you set a behavior using the API, the answer to the questions now becomes intersting, and changes the behavior at runtime of that method. Simple, but under the covers it's a lot of work:)

Isolator comes with a VS AddIn to make running the tests seamlessly within VS, and with a command line tool and MSBuild or NAnt tasks for usage in a build server.

I'll be happy to answer any other quesitions you may have.

Gil Zilberfeld [email protected]