473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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_Service abstract base
class that my Concrete_Communication_Service and my
Mock_Communication_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_Service* _communication;
public:
Foo(Communication_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 2414
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
TestDrivenDevelopment 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_Service abstract base
class that my Concrete_Communication_Service and my
Mock_Communication_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_Service* _communication;
public:
Foo(Communication_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********@news.boeing.com>,
Joe Van Dyk <jo********@boeing.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_Service abstract base
class that my Concrete_Communication_Service and my
Mock_Communication_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
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,...
4
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...
1
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"...
1
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
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
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...
5
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
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...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.