I have a record containing several properties.
I would like to assert properties of the record based on the type, not all using NUnit.
public record Employee(
int id,
string Name,
double Salary
....
)
I can assert properties of the record individually using Assert.That(result.Id, Is.EqualTo(id))
But would like to know if we can compare record to record excluding some properties of it based on type?
Like Assert.That(result, Is.EqualTo(employeeRecord).And.<Types within employeerRecord for comparision>)
Tried Assert.That(result.ToString(), Is.EqualTo(employeeRecord.ToString()))
However, in this case, we may not be able to compare the type of data, as all data gets converted to string.
In other words, Can I filter a record based on the type of data and then assert only specific fields within it?
Try this code.
[Test]
public void AddMethodTest1()
{
Record result= new Record() { id=1 , Name="name" ,Salary="salary"};
Record employeeRecord = new Record() { id = 1, Name = "name", Salary = "salary" };
Assert.AreEqual(new { result.Salary,result.Name }, new { employeeRecord.Salary,employeeRecord.Name });
}