Re: NUnit question

From: Naveen Mukkelli (NaveenMukkelli_at_discussions.microsoft.com)
Date: 02/04/05


Date: Thu, 3 Feb 2005 16:23:04 -0800

Hi,

  Can we use NMock to simulate concrete classes?.
  In my case, I have to a class with no interfaces and virtual methods.
  I've got 2 classes A, B, dependent on each other. That means, one
  Class A creates many objects of Class B.

  For example..

 public class DependenceClass
        {
                private string name = null;
                public DependenceClass(string str)
                {
                        this.name = str;
                }

                public DependenceClass()
                {
                }

                public void Wish()
                {
                        Console.WriteLine("Hello " + this.name );
                }
                public string GetName()
                {
                        return this.name;
                }
                public void SetName(string str)
                {
                        this.name = str;
                }
        }

        public class UnderTest
        {
                private DependenceClass dC = null;
                public UnderTest(DependenceClass dc)
                {
                        this.dC = dc;
                }
                public string GetName()
                {
                        return this.dC.GetName();
                }
                public void SetName(string str)
                {
                        this.dC.SetName(str);
                }
        }

my test class

[TestFixture]
        public class TestClass
        {

                private DependenceClass dependenceClass = null;

                [Test]
                public void Test1()
                {
                        IMock mock = new DynamicMock(
                                                                          
typeof(DependenceClass));
                        dependenceClass =
                                                            
(DependenceClass)mock.MockInstance;

                        mock.ExpectAndReturn("GetName","Naveen",null);
                        Assert.AreEqual
                                                        
("Naveen",dependenceClass.GetName());

        
                }

The test is failing and the excpetion is : System.ArgumentException, Method
GetName is not Virtual.

How can we test class with no vitual methods and Interfaces.

"David Levine" wrote:

> If you want to use NUnit, or unit testing in general, effectively, then you
> really can't directly test your code as it makes calls into the socket
> library. That's a valid test, but it's an integration test, not a unit test.
>
> A good unit test is repeatable on any machine, including build machines;
> this allows the unit tests to be run during the build process, and to halt
> the build if a test fails. The build machines often do not have all the
> tools and other infrastructure installed that the development or client
> machines have on them, so tests that rely on a particular configuration are
> more likely to fail. I like to keep the build machine as pure as possible to
> avoid dependency contamination.
>
> That being said, you can unit test your code, but you ought to create a mock
> object that mimics the behavior of the socket library. There are various
> open source tools, such as NMock, that make this easier. The purpose of the
> mock object is to simulate the behavior of the external dependency,
> including all possible errors that it can exhibit, so you can test your
> code's reaction to those behaviors. The mock object will expose the same API
> as the real object, and the code under test does not know which object it is
> dealing with. The unit test code creates a mock object and substitutes it
> for the "real" object before it runs the test.
>
> There are books and white papers on this subject - I'd suggest googling up
> some references and start reading. Once you get the hang of it it isn't
> hard.
>
> "Naveen Mukkelli" <NaveenMukkelli@discussions.microsoft.com> wrote in
> message news:333878B0-0CEB-4D40-B991-F96A92B98D3F@microsoft.com...
> > Hi,
> >
> > As I am accepting connections asynchronously in my server application,
> > hence
> > I would like to test asynchronous behaviour.
> >
> > I have not tried accepting sockets, synchronously, on the serve object.
> >
> > Can we test asynchronous connections at all ?
> >
> > Cheers,
> >
> > Naveen.
> >
> > "Carlos J. Quintero [.NET MVP]" wrote:
> >
> >> Have you try to accept the socket on the server object synchronously?
> >>
> >> --
> >>
> >> Carlos J. Quintero
> >>
> >> MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
> >> You can code, design and document much faster.
> >> http://www.mztools.com
> >>
> >>
> >> "Naveen Mukkelli" <NaveenMukkelli@discussions.microsoft.com> escribió en
> >> el
> >> mensaje news:6287ADED-1D97-48E1-ACCA-2963EB561D54@microsoft.com...
> >> > Hi all,
> >> >
> >> > How can we use "NUnit" in socket programming.
> >> > I mean, I'm writing a server program which accepts
> >> > connection requests from the clients. I want to test the
> >> > number of clients whenever a new connection is
> >> > established.
> >> >
> >> > My Server code is :
> >> > ---------------------------------------------------------------------
> >> > private string strHostName;
> >> > private IPAddress ipAddHost;
> >> > private Socket soketListener;
> >> > private int intPortNumber;
> >> > private int clients = 0;
> >> >
> >> > public Server(int portNumber)
> >> > {
> >> > this.intPortNumber = portNumber;
> >> > }
> >> >
> >> > public void StartListening()
> >> > {
> >> > strHostName = Dns.GetHostName();
> >> > IPHostEntry ipHostInfo = Dns.Resolve(strHostName);
> >> > ipAddHost = ipHostInfo.AddressList[0];
> >> >
> >> > socketListener.Bind( new IPEndPoint( ipAddHost,
> >> > intPortNumber ) );
> >> > socketListener.Listen( 100 );
> >> > socketListener.BeginAccept(
> >> > new AsyncCallback(
> >> > this.OnConnectionRequest ),socketListener );
> >> > }
> >> >
> >> > private void OnConnectionRequest(Socket client)
> >> > {
> >> > clients ++;
> >> > }
> >> >
> >> > public int GetClientCount()
> >> > {
> >> > return clients;
> >> > }
> >> >
> >> > -----------------------------------------------------------------------------
> >> > I have tried to use NUnit in the following way.
> >> >
> >> > [SetUp]
> >> > public void init()
> >> > {
> >> > Server server = new Server(portNumber);
> >> > }
> >> >
> >> > [Test]
> >> > public void Test()
> >> > {
> >> >
> >> > server.StartListening();
> >> >
> >> > Socket clientSocket = new Socket(
> >> > AddressFamily.InterNetwork, SocketType.Stream,
> >> > ProtocolType.Tcp );
> >> >
> >> > IPAddress localIp = IPAddress.Parse(ipAddress);
> >> > int portNumber = number;
> >> > IPEndPoint endPoint = new IPEndPoint(localIp,portNumber);
> >> >
> >> > socketClient.Connect(endPoint);
> >> >
> >> > Assert.AreEqual(1,server.GetClientCount());
> >> > }
> >> >
> >> > but test is failing... at
> >> > "Assert.AreEqual(1,server.GetClientCount());
> >> > the error message is expected <1> but was<0>
> >> >
> >> > can any one suggest me how can we use NUnit in this situation.
> >> >
> >> >
> >> > Cheers,
> >> >
> >> > Naveen Mukkelli.
> >> >
> >> >
> >>
> >>
> >>
>
>
>



Relevant Pages

  • Re: NUnit question
    ... That's a valid test, but it's an integration test, not a unit test. ... A good unit test is repeatable on any machine, including build machines; ... object that mimics the behavior of the socket library. ... The mock object will expose the same API ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: OT: software plug, open-source planetarium
    ... :Programmers on Linux/UNIX/BSD tend to have an egotistic mentality that ... :what exists on their machines should be on all machines. ... don't have to ship every dependency to them. ... "The reasonable man adapts himself to the world; ...
    (sci.military.naval)
  • Re: Strategy for Unit Testing Data Access Code
    ... Example" book from Addison Wesley is to use the Mock Object Pattern. ... > I'm using NUnit to unit test my VB.NET code. ... > - Also I've tried loading my datasets with special test data that is ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: NUnit question
    ... derive the real implementation from that, and define a mock object as well ... I have to a class with no interfaces and virtual methods. ... > public class DependenceClass ... The unit test code creates a mock object and substitutes it ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: SPS with SP2 vs. WPA2
    ... adds the group policy support and would, I presume, require ... dependency. ... None of our XP machines had 917021 installed, ... hasn't been delivered through WSUS or MU. ...
    (microsoft.public.windows.server.sbs)