C#: Help with Empty Collections Unit test

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • napstar
    New Member
    • Nov 2006
    • 55

    C#: Help with Empty Collections Unit test

    I have a C# program that returns an empty collection and a Unit test that test to see if the name of an element in the empty returned collection is the same as an object I have.I'm asserting that that a string comparison of both elments should be 0 but the test never fails,what am i doing wrong?:
    [Test]
    public void testGetProjects ()
    {
    Project1 = new Project(1);
    Project1.Name = "PHS";

    string tester;
    tester = "PHS";

    ReturnedProject s = PAgent.SearchPr ojects(tester);

    foreach (Project Proj in ReturnedProject s)
    {
    string nameOfProject;
    string projectNameToCo mpare;

    projectNameToCo mpare = Proj.Name;
    nameOfProject = Project1.Name;
    Assert.IsTrue(S tring.Compare(n ameOfProject, projectNameToCo mpare) == 0);


    }




    }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I am curious as to why you went to all these great lengths, I woulda just done this:
    Code:
    // [Test]
            public void testGetProjects()
            {
              Project1 = new Project(1);
              Project1.Name = "PHS";
              ReturnedProjects = PAgent.SearchProjects(tester);
              
              foreach (Project Proj in ReturnedProjects)
              {
                    if (proj.Name==Project1.Name)
                    {
                         //you have found if it is in there
                         //do whatever you want here
                         // then      break;
                    }
              }
            }
    That will tell you if the any project returned in the collection has the same name as the project.

    If you wanted to know if a particular instance of the project object was in the collection, you could just do
    if (Proj == Project1)
    {
    }

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      I have changed the title of this thread to better match the subject matter. Please, in the future, use descriptive titles. Doing so will ensure you get the help you are looking for.

      Comment

      Working...