473,789 Members | 2,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mock objects and testing

Say I've written a class that wraps around a particular (complex)
communication service library.

I want to unit test objects that use that communication service.

Is the "best practice" to create a Communication_S ervice abstract base
class that my Concrete_Commun ication_Service and my
Mock_Communicat ion_Service both inherit from? (and then use the mock
service when unit testing)

Or would I use templates somehow?

In Ruby, I'd create a mock communication class that just had the same
functions as the real communication class and, when testing, pass in the
mock communication object instead of the real communication object.
But, in C++, the mock and real object need to be of the same type, right?
Right now, I have

class Foo
{
Communication_S ervice* _communication;
public:
Foo(Communicati on_Service &comm) : _communication( &comm);
};

Then, in the test driver, I create Mock communication object and give
that to Foo's constructor. And in the real program, I create a real
communication object and give that to Foo's constructor.

(using unit testing when learning C++ is great, by the way. I can't
imagine doing it any other way.)

Thanks,
Joe
Apr 8 '06 #1
3 2444
Joe Van Dyk wrote:
Say I've written a class that wraps around a particular (complex)
communication service library.

I want to unit test objects that use that communication service.


I tossed this question onto the Yahoo Groups mailing list called
TestDrivenDevel opment because it's a gooder. Read the answers there.

(My day-job right now is to write unit tests that mock communication
services, so I myself would not want to divulge any trade secrets. Yeah,
that's the ticket;)

BTW you mock when you don't have a "seam". That's a place to insert tests.
The Sockets themselves make a perfect Seam, so plug your server and client
into a loopback socket, inside one test case, and there's your mocks.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 8 '06 #2
Joe Van Dyk wrote:
Say I've written a class that wraps around a particular (complex)
communication service library.

I want to unit test objects that use that communication service.

Is the "best practice" to create a Communication_S ervice abstract base
class that my Concrete_Commun ication_Service and my
Mock_Communicat ion_Service both inherit from? (and then use the mock
service when unit testing)
Another alternative, depending on you code layout, is to provide a mock
version of the class implementation in your test code and the real one
in a library the test code for the clients doesn't link.
Or would I use templates somehow?

In Ruby, I'd create a mock communication class that just had the same
functions as the real communication class and, when testing, pass in the
mock communication object instead of the real communication object. But,
in C++, the mock and real object need to be of the same type, right?
Right now, I have

class Foo
{
Communication_S ervice* _communication;
public:
Foo(Communicati on_Service &comm) : _communication( &comm);
};

Then, in the test driver, I create Mock communication object and give
that to Foo's constructor. And in the real program, I create a real
communication object and give that to Foo's constructor.
A common solution.
(using unit testing when learning C++ is great, by the way. I can't
imagine doing it any other way.)

Not just when learning.

--
Ian Collins.
Apr 8 '06 #3
In article <Ix********@new s.boeing.com>,
Joe Van Dyk <jo********@boe ing.com> wrote:
Say I've written a class that wraps around a particular (complex)
communication service library.

I want to unit test objects that use that communication service.

Is the "best practice" to create a Communication_S ervice abstract base
class that my Concrete_Commun ication_Service and my
Mock_Communicat ion_Service both inherit from? (and then use the mock
service when unit testing)

Or would I use templates somehow?


First we have to ask some questions:

Given class B uses an object of type A (ie some object that conforms to
the interface of type A.)

Do we know, at compile time, which types of A's are used by our Bs?

Do different B objects use different sub-types of A?

If the answer to the above is "yes", do B objects change which sub-type
of A object they use during their lifetimes?

If the answer to the first question is "no" or the answer to all three
questions are "yes", then I suggest:

class B {
A* a;
public:
B( A* a_ ): a( a_ ) { }
void changeA( A* a_ ) { a = a_; } // if answer 3 is "yes"
// ...
};

This is the only possible solution because we can't say what sub-type of
A, any particular B will use until after the program starts running. Be
careful of lifetime issues though. (for example, ~B and 'changeA' might
need to delete 'a'.)
If the answers are "yes, yes, no", then I suggest:

template < typename A >
class B {
A a;
public:
// ...
};

Here different B's can use different kinds of A's.

If the answers are "yes, no, no", (I suspect this is the case for you)
then the above solutions are overkill. Simply do this:

#include "A.h"

class B {
A a;
public:
//...
};

In your test code, you would use one "A.h" file, in your production code
you use a different "A.h" file.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1976
by: John J. Lee | last post by:
I had the bright idea a week or two ago to construct mock objects (for unit testing) "by example" by calling methods on a generic mock object. Say we have a class Bar, and we want to check that, when we use BarUser in a particular way, BarUser first calls method Bar.xyzzy with args "foo" and bar=2, then method Bar.do_nothing. # first, construct a mock object by example: m = Mock()
4
1856
by: John J. Lee | last post by:
I'm trying define a class to act as a Mock "handler" object for testing urllib2.OpenerDirector. OpenerDirector (actually, my copy of it) does dir(handler.__class__) to find out what methods a handler supports. So, my mock class has to have appropriate methods defined on it. To avoid the hassle of manually defining lots of mock classes, I wanted to have a base class MockHandler so that I could do class...
1
1788
by: ischenko | last post by:
There is a (relatively) widely used technique in unit testing, called mock objects. There is even a pMock library which provides a Mock class for a Python environment. Given the "duck typing" nature of the Python itself, it's pretty trivial to build mocks without using any pre-built libraries. What is less trivial and potentially more worthwhile is to have a library of "stock" mock objects. For instance, I found myself re-implementing...
1
1751
by: Kutty Banerjee | last post by:
Hi, curious if there is a tool available for creating Mock Objects in C++ the JAVA style. kutty
4
2392
by: David Thielen | last post by:
Hi; Are there mock objects anywhere that I can use to write nunit tests for my code behind methods? -- thanks - dave david_at_windward_dot_net http://www.windwardreports.com
2
3252
by: Terry | last post by:
I'm looking at NMock2 as a framework to create mock objects during my unit testing. One part that I'm trying to understand is that it will mock interfaces, not concrete classes. I normally don't create interfaces for every class. For example I normally don't have an interface for my business object classes. A business object class is going to have too many unique methods that I don't see the point of creating an interface that will only...
5
5409
by: Pawel Pabich | last post by:
Hi, I need to mock Stream.Read method with RhinoMocks but it looks like it's impossible. Any ideas?
1
2253
by: earthwormgaz | last post by:
Has anyone seen a tool for C++ that automatically creates mock classes? I found MockMaker for Java, but I wondered if there was something similar for us C++ developers. The idea is to read in the interface, and then spit out a version of that that is suited to testing.
0
1075
by: Fuzzyman | last post by:
Mock 0.4.0 has just been released, the first release in about ten months (but worth the wait). Mock is a simple library for testing: specifically for mocking, stubbing and patching. * Mock Homepage & Documentation http://www.voidspace.org.uk/python/mock.html * mock.py (module only) <http://www.voidspace.org.uk/downloads/mock.py * mock-0.4.0.zip (module, tests and documentation) <http:// www.voidspace.org.uk/downloads/mock-0.4.0.zip
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4089
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.