title: Generating copies with Autofixture
body: <p>I'm using Autofixture for my unit tests, with auto-generated data.</p>
  
  <p>To test a simple Controller endpoint (Get employee by Id), I'm doing something similar to this:</p>
  
  <pre><code>[Theory, AutoData]
  public void GetEmployeeById_ValidId_ReturnsExpectedModel(
      EmployeeModel expectedEmployee,
      [Frozen] Mock&lt;IEmployeeService&gt; employeeServiceMock,
      EmployeesController sut)
  {
      employeeServiceMock
          .Setup(x =&gt; x.GetEmployeeById(42))
          .Returns(expectedEmployee);
  
      var actual = sut.GetEmployeeById(42);
  
      actual.As&lt;OkObjectResult&gt;().Value.As&lt;EmployeeModel&gt;()
          .Should().BeEquivalentTo(expectedEmployee);
  }
  </code></pre>
  
  <p>And the controller:</p>
  
  <pre><code>[HttpGet("{id:int}")]
  public IActionResult GetEmployeeById(int id)
  {
      var employee = employeeService.GetEmployeeById(id);
      if (employee == null)
          return NotFound("Employee not found");
  
      return Ok(employee);
  }
  </code></pre>
  
  <p>In this unit test, <code>expectedEmployee</code> is auto-generated with 'random' data. The sut (System Under Test) is configured to be generated with all its required dependencies (one of them is <code>IEmplyeeService</code>).</p>
  
  <p>The problem with this unit test is that, if I change the employee before returning it from the controller, the test would still pass (because it's referencing the same object):</p>
  
  <pre><code>employee.SomeInternalModel.FooProperty = "Foo";
  return Ok(employee);
  </code></pre>
  
  <p>So, I consider the above unit test to be bad.</p>
  
  <p>To make the unit test fail in this scenario, I need to pass a separate object: deep copy of the <code>EmployeeModel</code>:</p>
  
  <pre><code>employeeServiceMock
      .Setup(x =&gt; x.GetEmployeeById(42))
      .Returns(expectedEmployee.DeepCopy());
  </code></pre>
  
  <p>I don't have time and resources to write deep copy methods for all my models.</p>
  
  <p><strong>How to easily auto-generate identical models?</strong> I thought about seed-ing the <code>AutoFixture</code> but it doesn't seem to support this feature.</p>
  
  <p>Do you have any <strong>elegant</strong> suggestions?</p>