2

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?

Jason Learmouth
  • 569
  • 5
  • 9
  • initial glance of the test shows that you target `ViewResult.Model` which is type `object`. Try casting the model first then trying the assertion so you can take advantage of the generics. Also if using async in the test make the test method `async Task` and not `async void` – Nkosi Jan 08 '18 at 01:50
  • thanks @Nkosi, I'm now casting the `ViewResult.Model`, but I still don't know how to tell FA that the field has a different name in the Model, and that it is the inverse value. – Jason Learmouth Jan 08 '18 at 02:32
  • Is the view returning a single view model or a list? You cast to a single view model but are comparing it to a list of models. I am still looking into whether the framework can do what it is you are trying to achieve – Nkosi Jan 08 '18 at 02:36
  • Yes, it should be a List. – Jason Learmouth Jan 08 '18 at 02:43
  • Read up on `ShouldAllBeEquivalentTo` for comparing collections – Nkosi Jan 08 '18 at 02:49
  • 1
    I have read [this documentation](http://fluentassertions.com/documentation.html#collections) many times over the last couple weeks. [This question](https://stackoverflow.com/questions/41699863/using-shouldbeequivalentto-and-handling-different-names) seems most closely related to what I'm trying to do, the solution there was to exclude those properties and compare them separately. I think that's what I'll do too. – Jason Learmouth Jan 08 '18 at 03:38

0 Answers0