I have a model with a boolean property called "InActiveFlag" which comes from a third party API. This gets mapped to a property called "IsActive" in my view model. "IsActive" is the inverse of "InActiveFlag"
Here is the ViewModel Class:
public class ViewModel
{
public int Id { get; set; }
public string Description { get; set; }
[Display(Name = "Is Active")]
public bool IsActive { get; set; }
}
And this is the Model Class:
public class Model
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("inactiveFlag")]
public bool InactiveFlag { get; set; }
}
I'm trying to get this comparison of a List<ViewModel>
to a List<Model>
working using the options parameter of ShouldBeEquivalent, but I haven't been able to figure it out.
My controller uses AutoMapper to convert a List<Modle>
to List<ViewModel>
like this:
List<ViewModel> viewModel = _mapper.Map<List<Model>>(listOfModels);
In my controller tests I am passing a list of Models (testModel
) to the controller. I then want to test that the correct and complete List<ViewModel>
is used.
Here is the test code:
[Fact]
async void GetMethodViewModelIsListReturnedByApiCallbacksGet()
{
SetupControllerApiReturnsListOf100Models();
var viewResult = (ViewResult)await controller.Get();
List<ViewModel> viewModel = (List<ViewModel>)viewResult.Model;
viewModel.ShouldBeEquivalentTo(ListOfTestModels, options =>
options.Using<ViewModel>(ctx => ctx.Subject.IsActive.ShouldNotBe(ListOfTestModels.InActiveFlag)));
}
but this doesn't even compile...
I've looked at the docs, but the options.Using
example only modifies the existing expectation, I need to replace it because the field name is different.
How do I tell FluentAssertions to compare a property of the subject to a particular property of the expectation parameter and override the same name convention?