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" }
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 }";
}