Mocking non-virtual methods in C# [duplicate]
Asked Answered
C

2

13

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

Coastline answered 2/7, 2009 at 10:43 Comment(2)
Best guess, a combination of AppDomains, Reflection.Emit and a fair bit of hackery and magic. But I'm hardly qualified to make many educated guesses about this.Heeler
See also #2188013Sisyphus
S
14

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 questions you may have.

Sidney answered 3/7, 2009 at 14:11 Comment(1)
You've got to be really good not to cut yourself when using real sharp knives! ;). I guess that's what they meant by the "too powerful" comments.Hysteroid
L
7

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 too 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 :)

Leu answered 2/7, 2009 at 11:0 Comment(2)
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"Adamski
Well, I guess the main scenarios for typemock is if you need to work with a legacy system or 3rd party where you can't (easily) change the design and you still want to add unit tests and mock out certain things.Iluminadailwain

© 2022 - 2024 — McMap. All rights reserved.