Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assert.Contains doesn't find the object in a list

I’m new to unit testing, and I wanted to test a method that gets a string array that contains some names like "John,Doe" and then it splits the name by the "," and populates a list of PersonModel with those names, so I expect that there is a PersonModel with first name John, and last name Doe, in the returned list, but the Assert.Contains method keeps throwing this error:

Assert.Contains() Failure

Not found: PersonModel { FirstName = "John", FullName = "John Doe",
LastName = "Doe" }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In value: List [PersonModel {
FirstName = "John", FullName = "John Doe", LastName = "Doe" },
PersonModel { FirstName = "Jane", FullName = "Jane Doe", LastName =
"Doe" }]

This is the method for converting names to people list in the DataAccess class:

public static List<PersonModel> ConvertCsvNamesToPeopleList(string[] csvContent)
{
    List<PersonModel> output = new List<PersonModel>();

    foreach (string line in csvContent)
    {
        string[] data = line.Split(',');
        output.Add(new PersonModel { FirstName = data[0], LastName = data[1] });
    }

    return output;
}

And this is the test:

[Fact]
public void ConvertCsvNamesToPeopleList_ValidCsvContent_ShouldWork()
{
    string[] csvContent = { "John,Doe", "Jane,Doe" };

    var expectedPerson = new PersonModel { FirstName = "John", LastName = "Doe" };
    var expectedPerson2 = new PersonModel { FirstName = "Jane", LastName = "Doe" };
    var actual = DataAccess.ConvertCsvNamesToPeopleList(csvContent);

    Assert.Contains(expectedPerson, actual);
    Assert.Contains(expectedPerson2, actual);
}

The person model:

public class PersonModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName => $"{ FirstName } { LastName }";
}

>Solution :

Contains relies on finding two objects that are equal, so you should override its Equals method. For good measures, you should override its GetHashCode method too, so you can use it as a key of a dictionary if you ever need to:

public override bool Equals(object obj)
{
    return obj is PersonModel model &&
           FirstName == model.FirstName &&
           LastName == model.LastName;
}

public override int GetHashCode()
{
    return HashCode.Combine(FirstName, LastName);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading