unit testing client/server application question.

G

Guest

Hi,

I'm writing a client/server application using C#.
The server accepts connecitons asynchronously, using BeginAccept/EndAccept.

Is there any way we can write some unit tests(NUnit) to test the behaviour
of
accepting connections and testing some other private methods that would be
called when the server receives a connection request.

How I can use NMock objects in this scenario. Kindly let me know.

Cheers,

Naveen.
 
A

Alan Pretre

Naveen Mukkelli said:
Is there any way we can write some unit tests(NUnit) to test the
behaviour
of
accepting connections and testing some other private methods that would
be
called when the server receives a connection request.

I would suppose that using the TextFixtureSetup() and TextFixtureTeardown()
attributes you could write test routines to access the server?

-- Alan
 
N

Nick Malik [Microsoft]

Testing an object that calls another downstream object is always a bit
tricky with any unit testing framework. NUnit is an example.

This problem, faced and fixed, over and over, led to much of the current
interest in IoC (Inversion of Control) patterns and then to AOC (Aspect
Oriented Programming). In other words, once you get the hang of this, you
will find that you are developing code in a different way, and other ideas
of OO development will become more natural.

What isn't clear from your message is this: are you testing the client
object that calls the server, or the server object that is being called?

I would STRONGLY recommend that you test one without testing the other.

The key is to look for the keyword 'new'. 'new' is your enemy. First learn
to avoid it, then conquer it.

So your code may look like this:
public class MyClass
{
public int MyMethod(int parm1, string parm2)
{
MyServerObject mso = new MyServerObject(); // <---
really really really bad
mso.StartCall();
// do some stuff
mso.EndCall();
}
}

Easy Refactor: Place an attribute at the class level, or a mechanism in the
constructor, to "own" the dependency.
public class MyClass
{
private MyServerObject _mso;
public MyServerObject mso
{ set { _mso = value; }
get {
if (_mso == null)
return new MyServerObject();
else
return _mso;
}
}

public int MyMethod(int parm1, string parm2)
{
// MyServerObject mso = new MyServerObject(); // <---
COMMENTED OUT
mso.StartCall();
// do some stuff
mso.EndCall();
// since it no longer leaves scope, it is up to you if
you need to release mso specifically
}
}

Two things to note: Normal calling code doesn't change.

However, your unit test code can be different...
In your unit test code:
public void MyTest()
{
int returncode;
MyMockServerObject mmso = new MyMockServerObject();
MyClass mc = new MyClass();
mc.mso = mmso; // not something you do in the normal
code!
returncode = mc.MyMethod(1,"a");
// test the return, validate methods, yada yada yada
}

So where do you get this "MyMockServerObject" class? Well, either you
create an interface that both the actual server object and the mock will
derive from, or you create the MyMockServerObject class in your unit test
code by inheriting from the MyServerObject class, and you override all the
methods. Depends on whether you can change the source for the
MyServerObject class.

Note: this didn't leverage NMock at all. You needed to get the basics
before the automation makes sense. Try this. Once you understand it, at an
automatic level, NMock will make sense.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top