Search code examples
c#unit-testingautofixtureautomoq

AutoFixture deep freeze of object


I have a class

public class GetDashboardStatisticsResult
{
  public GetPublicationStatisticsResult Publications { get; set; }
  public GetSwitchboardStatisticsResult Switchboard { get; set; }
}

Which I use in my test like this

public async Task Should_return_correct_statistics([Frozen] GetDashboardStatisticsResult expectedResult);

And I wonder if there is there a way to freeze GetDashboardStatisticsResult together with its properties?

So at the end we have three types frozen - GetDashboardStatisticsResult, GetPublicationStatisticsResult and GetSwitchboardStatisticsResult?


Solution

  • I don't think AutoFixture has any feature that enables something like that, but you could, possibly, work around it like this:

    [Theory, AutoData]
    public async Task Should_return_correct_statistics(
        [Frozen]GetPublicationStatisticsResult dummy1,
        [Frozen]GetSwitchboardStatisticsResult dummy2,
        [Frozen]GetDashboardStatisticsResult expectedResult)
    {
        // Test goes here...
    }
    

    The best solution is probably to reconsider the design of the types in question. I've never encountered needing such a feature. What problem are you trying to solve?