9

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.

RAM
  • 2,257
  • 2
  • 19
  • 41
Alok Save
  • 202,538
  • 53
  • 430
  • 533

4 Answers4

3

Fake-It is a simple mocking framework for C++. FakeIt uses the latest C++11 features to create an expressive (yet very simple) API. With FakeIt there is no need for re-declaring methods nor creating a derived class for each mock. Here is how you Fake-It:

struct SomeInterface {
  virtual int foo(int) = 0;
};

// That's all you have to do to create a mock.
Mock<SomeInterface> mock; 

// Stub method mock.foo(any argument) to return 1.
When(Method(mock,foo)).Return(1);

// Fetch the SomeInterface instance from the mock.
SomeInterface &i = mock.get();

// Will print "1"
cout << i.foo(10);

There are many more features to explore. Go ahead and give it a try.

Eran Pe'er
  • 511
  • 5
  • 5
3

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.

Anon
  • 1,290
  • 2
  • 16
  • 24
3

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:

I hope this helps.

Thanks, Damian

Damian Schenkelman
  • 3,505
  • 1
  • 15
  • 19
2

Google Mock is a framework for mocking of dependencies of the class being unit tested. The website also includes a good introduction.

Sandeep
  • 648
  • 5
  • 12