I am writing unit tests with Specflow 3, to test a method that query data in db and I use NFluent to have nice assertions methods.
Is there any way to smoothly test equality over a few members of an object ?
There is a method called HasFieldsWithSameValues but I found nothing in their wiki that explains how to use it thoroughly. There is a Excluding("fieldNames","etc",...) method that we can chain, but I would have to exclude over 20 public members.
Here is my feature file ( functionnal scenario, Gherkin syntax )
Scenario: TimeSheet_001_SearchForExistingTimeSheet
When I search for timesheet with id 31985
Then I get the following timesheet
| id | StatutPaid | StatutFact |
| 31985 | Paid | None |
Then I have the methods called by this code bloc :
[When(@"I search for timesheet with id (.*)")]
public void WhenJeChercheLeReleveDHeureAvecLId(int id)
{
var result = GetTimeSheetDetailQuery(id);
timeSheetContext.Result = result;
}
[Then(@"I get the following timesheet")]
public void ThenJObtiensLeReleveDHeureSuivant(Table table)
{
var sut = table.CreateInstance<TimeSheetDetail>();
Check.That(sut).HasFieldsWithSameValues<TimeSheetDetail>().Fields.IsEqualTo(timeSheetContext.Result);
}
The problem is that TimeSheetDetail is a VERY detailed object, I just need to check for 3 members for this case of test. is there a way to write for example
//this
Check.That(sut)
.HasFieldsWithSameValues<myType>("Id","StatusPaid","StatusFact")
.IsEqualTo(timeSheetContext.Result);
//instead of this
Check.That(sut.Id).IsEQualTo(timeSheetContext.Result.Id);
Check.That(sut.StatusPaid).IsEQualTo(timeSheetContext.Result.StatusPaid);
Check.That(sut.StatusFact).IsEQualTo(timeSheetContext.Result.StatusFact);
From the sound of things, the exact functionality that you're looking for isn't directly supported by NFluent. This sounds like a good candidate for using C# extension methods, and fortunately NFluent supports extension methods.
The gist of the idea is that you can implement a method that contains the logic that you're trying to have, and this method can be directly called by NFluent in order to achieve the syntax that's closer to what you're looking for (if not exactly what you're looking for). After implementing such method, you'll be able to make a call like so:
Check.That(sut).HasFieldsWithSameValues(timeSheetContext.Result, "Id", "StatusPaid", "StatusFact");
The extension method's prototype would be something like this:
public static ICheckLink<ICheck<Object>> HasFieldsWithSameValues(this ICheck<Object> check, myType myObject, params string[] fieldsList)
Note that you may be able to get the exact syntax that you're looking for, but you'll have to do a little bit more work to figure out how to do that in the context of extension methods.
Find out more about C# extension methods here.
Find an example of how to implement NFluent extension methods here.