views:

444

answers:

4
+7  Q: 

What is Mocking?

What is Mocking?

+2  A: 

There are plenty of answers on SO and good posts on the web about mocking. One place that you might want to start looking is the post by Martin Fowler Mocks Aren't Stubs where he discusses a lot of the ideas of mocking.

In one paragraph - Mocking is one particlar technique to allow testing of a unit of code with out being reliant upon dependencies. In general, what differentiates mocking from other methods is that mock objects used to replace code dependencies will allow expectations to be set - a mock object will know how it is meant to be called by your code and how to respond.


Your original question mentioned TypeMock, so I've left my answer to that below:

TypeMock is the name of a commercial mocking framework.

It offers all the features of the free mocking frameworks like RhinoMocks and Moq, plust some more powerful options.

Whether or not you need TypeMock is highly debatable - you can do most mocking you would ever want with free mocking libraries, and many argue that the abilities offered by TypeMock will often lead you away from well encapsulated design.

As another answer stated 'TypeMocking' is not actually a defined concept, but could be taken to mean the type of mocking that TypeMock offers, using the CLR profiler to intercept .Net calls at runtime, giving much greater ability to fake objects (not requirements such as needing interfaces or virtual methods).

David Hall
@Masoud never mentioned TypeMock. His question was about "type mocking" in general.
Peter Lillevold
@Peter - as another comment said, check the edit history of the question. Not a lot I can do if I post an answer and then the original question is completely changed.
David Hall
Thank you David.
masoud ramezani
+1  A: 

I would think the use of the TypeMock isolator mocking framework would be TypeMocking.

It is a tool that generates mocks for use in unit tests, without the need to write your code with IoC in mind.

Oded
@Masoud never mentioned TypeMock. His question was about "type mocking" in general.
Peter Lillevold
Actually, the original question included the word "Type" before "Mocking", but it was later edited out. That is why some of the answers contains specific information about TypeMock.
Martin Liversage
+1  A: 

The purpose of mocking types is to sever dependencies in order to isolate the test to a specific unit. Stubs are simple surrogates, while mocks are surrogates that can verify usage. A mocking framework is a tool that will help you generate stubs and mocks.

EDIT: Since the original wording mention "type mocking" I got the impression that this related to TypeMock. In my experience the general term is just "mocking". Please feel free to disregard the below info specifically on TypeMock.

TypeMock Isolator differs from most other mocking framework in that it works my modifying IL on the fly. That allows it to mock types and instances that most other frameworks cannot mock. To mock these types/instances with other frameworks you must provide your own abstractions and mock these.

TypeMock offers great flexibility at the expense of a clean runtime environment. As a side effect of the way TypeMock achieves its results you will sometimes get very strange results when using TypeMock.

Brian Rasmussen
@Masoud never mentioned TypeMock. His question was about "type mocking" in general.
Peter Lillevold
@Peter: The original wording was "what is type mocking?".
Brian Rasmussen
I know. Since "type mocking" is not equivalent to "TypeMock", I find both yours and @Oded answer quite off the mark.
Peter Lillevold
@Peter: In my experience the general term is "mocking", but in any case I have updated my answer to hopefully make that clear. Thanks for the input.
Brian Rasmussen
@Brian: no problem :)
Peter Lillevold
Thank you Brian.
masoud ramezani
+11  A: 

Mocking is primarily used in unit testing. An object under test may have dependencies on other (complex) objects. To isolate the behavior of the object you want to test you replace the other objects by mocks that simulate the behavior of the real objects. This is useful if the the real objects are impractical to incorporate into the unit test.

In short, mocking is creating objects the simulate the behavior of real objects.


On Stackoverflow you can find a question about .NET mocking frameworks.

At times you may want to distinguish between mocking as opposed to stubbing. There may be some disagreement about this subject but my definition of a stub is a "minimal" simulated object. The stub implements just enough behavior to allow the object under test to execute the test.

A mock is like a stub but the test will also verify that the object under test calls the mock as expected. Part of the test is verifying that the mock was use correctly.

To give an example: You can stub a database by implementing a simple in-memory structure for storing records. The object under test can then read and write records to the database stub to allow it to execute the test. This could test some behavior of the object not related to the database and the database stub would be included just to let the test run.

If you instead want to verify that the object under test writes some specific data to the database you will have to mock the database. Your test would then incorporate assertions about what was written to the database.

Martin Liversage
Thank you Martin.
masoud ramezani