Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock objects in C++

What are Mock objects? Can you please explain the concept? How to make use of Mock objects in C++? Any source examples will be highly helpful.

like image 455
Alok Save Avatar asked Sep 28 '10 03:09

Alok Save


People also ask

What are mock objects?

In object-oriented programming, a mock object is a simulated object that mimics the behavior of the smallest testable parts of an application in controlled ways.

What is mock in C?

A mock is a simulated function or module that mimics the behavior of a real implementation but is fully controlled by the unit test itself. The programmer can validate how many times a mock is called and verify the value of all arguments passed into the mock.

What is mock object C#?

Mocking is a process that allows you to create a mock object that can be used to simulate the behavior of a real object. You can use the mock object to verify that the real object was called with the expected parameters, and to verify that the real object was not called with unexpected parameters.

What happens when we mock an object?

A Mock object is one kind of a Test Double. You are using mockobjects to test and verify the protocol/interaction of the class under test with other classes. Typically you will kind of 'program' or 'record' expectations : method calls you expect your class to do to an underlying object.


2 Answers

In general, a mock object is referring to an instance of a class that as the name says "mocks" the functionality of the original class. This is usually simplified when coding against an interface, so when testing a component that depends on an interface, you simply implement the interface to return the results necessary to perform your tests.

You can find more information here, including the different kinds of mocks that are used for testing:

  • http://msdn.microsoft.com/en-us/magazine/cc163904.aspx
  • http://msdn.microsoft.com/en-us/magazine/cc163358.aspx

I hope this helps.

Thanks, Damian

like image 132
Damian Schenkelman Avatar answered Sep 21 '22 12:09

Damian Schenkelman


Read up on mockcpp and you'll find the answers to your question. Mocks are great for testing purposes where you can focus on testing one thing and mocking the behavior of other pieces in the environment.

like image 36
Anon Avatar answered Sep 18 '22 12:09

Anon