By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,712 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,712 IT Pros & Developers. It's quick & easy.

unit testing client/server application question.

P: n/a
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.
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Naveen Mukkelli" <Na************@discussions.microsoft.com> wrote in
message news:0B**********************************@microsof t.com...
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
Nov 17 '05 #2

P: n/a
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.
--
"Naveen Mukkelli" <Na************@discussions.microsoft.com> wrote in
message news:0B**********************************@microsof t.com...
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.

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.