471,122 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

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?:
public void testGetProjects()
Project1 = new Project(1);
Project1.Name = "PHS";

string tester;
tester = "PHS";

ReturnedProjects = PAgent.SearchProjects(tester);

foreach (Project Proj in ReturnedProjects)
string nameOfProject;
string projectNameToCompare;

projectNameToCompare = Proj.Name;
nameOfProject = Project1.Name;
Assert.IsTrue(String.Compare(nameOfProject, projectNameToCompare) == 0);


Jun 7 '07 #1
2 1535
7,872 Expert 4TB
I am curious as to why you went to all these great lengths, I woulda just done this:
Expand|Select|Wrap|Line Numbers
  1. // [Test]
  2.         public void testGetProjects()
  3.         {
  4.           Project1 = new Project(1);
  5.           Project1.Name = "PHS";
  6.           ReturnedProjects = PAgent.SearchProjects(tester);
  8.           foreach (Project Proj in ReturnedProjects)
  9.           {
  10.                 if (proj.Name==Project1.Name)
  11.                 {
  12.                      //you have found if it is in there
  13.                      //do whatever you want here
  14.                      // then      break;
  15.                 }
  16.           }
  17.         }
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)
Jun 7 '07 #2
3,237 Expert 2GB
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.
Jun 7 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Roy Smith | last post: by
3 posts views Thread by steven | last post: by
2 posts views Thread by Naveen Mukkelli | last post: by
reply views Thread by Benjamin Bittner | last post: by
reply views Thread by Ewart MacLucas | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.