unit testing client/server application question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Naveen Mukkelli

    unit testing client/server application question.

    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.
  • Alan Pretre

    #2
    Re: unit testing client/server application question.

    "Naveen Mukkelli" <NaveenMukkelli @discussions.mi crosoft.com> wrote in
    message news:0B8C1F1A-50B3-4F0C-A801-9C29732AA531@mi crosoft.com...[color=blue]
    > 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.[/color]

    I would suppose that using the TextFixtureSetu p() and TextFixtureTear down()
    attributes you could write test routines to access the server?

    -- Alan


    Comment

    • Nick Malik [Microsoft]

      #3
      Re: unit testing client/server application question.

      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;
      MyMockServerObj ect mmso = new MyMockServerObj ect();
      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 "MyMockServerOb ject" class? Well, either you
      create an interface that both the actual server object and the mock will
      derive from, or you create the MyMockServerObj ect 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


      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" <NaveenMukkelli @discussions.mi crosoft.com> wrote in
      message news:0B8C1F1A-50B3-4F0C-A801-9C29732AA531@mi crosoft.com...[color=blue]
      > 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.[/color]


      Comment

      Working...