Advertisement

07.11.2008 at 12:19AM PDT, ID: 23556401
[x]
Attachment Details

C#, Nunit framework--UML

Asked by niceguy971 in C# Programming Language, Microsoft Visual C#.Net

Tags: C#, Nunit framework

I have the  class below--UserInfo. I need to use the NUnit Framework to test my class (the web-site www.nunit.org).

1. Am I using NUnit Framework correctly??
2. How can I perform 4th test b1==null ??
3. Am I performing test 1, 2,3, 5.6, 7 correctly??
Thanks

using System;
using System.Collections;
using System.Runtime.Serialization;
using NUnit.Framework;


namespace abc
{
      /// <summary>
      /// Summary
      /// </summary>

      [Serializable()]
      public class UserInfo
      {
            private String _name;      //              
            private String _userId;      //             

            public String Name            
            {                  
                  get { return _name; }                  
                  set { _name = value; }            
            }             


            public String UserId            
            {                  
                  get { return _userId; }                  
                  set { _userId = value; }            
            }             
            /// <summary>            
            /// Default constructor necessary for             
            /// XML serialization            
            /// </summary>
            public UserInfo()            
            {                  
                  _name = String.Empty;                  
                  _userId = String.Empty;            
            }             

            public UserInfo(String name, String userId)            
            {                  
                  this._name = name;                  
                  this._userId = userId;            
            }             
   
            public override bool Equals(object obj)            
            {                  
                  if ((object)obj == null)                        
                        return false;                   

                  if(!(obj is UserInfo))                        
                        return false;                   

                  return (this._name == (obj as UserInfo)._name) && (_userId == (obj as UserInfo)._userId);            
            }             

            public override string ToString()            
            {                  
                  return String.Format("{0}: {1}", _userId, _name);            
            }             


            public override int GetHashCode()            
            {                  
                  return this.ToString().GetHashCode();            
            }      

      }

      // Start Nunit HERE

      

      [TestFixture]
      public class TestCase
      {
       UserInfo b1;
         UserInfo b2;
         UserInfo b3;
         UserInfo b4;

            [SetUp]
            public void Init()
            {
           b1 = new UserInfo("Bill Smith", "BSMITH");
               b2 = new UserInfo("Bill Smith", "BSMITH");
               
               b3 = new UserInfo("Susan Smith", "SSMITH");
               b4 = new UserInfo(null,null);

               string str="Some String"; // for test #5

                  Hashtable ht = new Hashtable(); //test #7
                  ht.Add(b1,"Some Data");
                  String s = (String) ht[b1];
            }

            [Test]
            public void CompareObj()
            {
                // Test b1==b1  /1st test
                  System.Console.WriteLine( b1.Equals(b1) ); // prints true
                  Assert.AreEqual("Bill Smith",b1.Name);
            Assert.AreEqual("BSMITH",b1.UserId);

                  // 2nd Test b1==b2
                  System.Console.WriteLine( b1.Equals(b2) ); // prints true
            Assert.AreEqual("Bill Smith",b1.Name);
                  Assert.AreEqual("Bill Smith",b2.Name);

            Assert.AreEqual("BSMITH",b1.UserId);
                  Assert.AreEqual("BSMITH",b2.UserId);
      
                  //3rd Test b1==b3
                  System.Console.WriteLine( b1.Equals(b3) ); // prints false
                  Assert.AreEqual("Bill Smith",b1.Name);
                  Assert.AreEqual("Bill Smith",b3.Name);

                  Assert.AreEqual("BSMITH",b1.UserId);
                  Assert.AreEqual("BSMITH",b3.UserId);

                  // 4th test b1==null
                  System.Console.WriteLine( b1.Equals(null) ); // prints false
                  // How can I test it using nunit???
            


                  // 5th test b1=="Some String"
                  System.Console.WriteLine( b1.Equals("Some String") );
            Assert.AreEqual(false,(str is UserInfo)); // Is it correct???

                  // 6th test b4==b1
                  System.Console.WriteLine( b4.Equals(b1) ); // prints false
                  Assert.AreEqual(null,b4.Name); // Is it correct???
                  Assert.AreEqual(null,b1.Name);

                  Assert.AreEqual(null,b4.UserId); // Is it correct???
                  Assert.AreEqual(null,b1.UserId);

                  // 7th test
            System.Console.WriteLine( s.Equals("Some Data") ); // prints true
            Assert.AreEqual("Some Data",s); // Is it correct???
            }
      
      } // End class TestCase
      
      // End Nunit HERE
      
} //End Namespace

Start Free Trial
[+][-]07.11.2008 at 02:51AM PDT, ID: 21981016

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C# Programming Language, Microsoft Visual C#.Net
Tags: C#, Nunit framework
Sign Up Now!
Solution Provided By: scorpiia
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07.11.2008 at 02:53AM PDT, ID: 21981022

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.11.2008 at 05:45AM PDT, ID: 21981810

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628