title: Mocking an attribute change on a parameter - using Moq
body: <p>I am using Moq to mock my Repository layer so I can unit test.</p>
  
  <p>My repository layer Insert methods update the Id property of my entities when a successful db insert occurs.</p>
  
  <p>How do I configure moq to update the Id property of the entity when the Insert method is called?  </p>
  
  <p>Repository code:-</p>
  
  <pre><code>void IAccountRepository.InsertAccount(AccountEntity account);
  </code></pre>
  
  <p>Unit Test:-</p>
  
  <pre><code>[TestInitialize()]
  public void MyTestInitialize() 
  {
      accountRepository = new Mock&lt;IAccountRepository&gt;();
      contactRepository = new Mock&lt;IContactRepository&gt;();
      contractRepository = new Mock&lt;IContractRepository&gt;();
      planRepository = new Mock&lt;IPlanRepository&gt;();
      generator = new Mock&lt;NumberGenerator&gt;();
  
      service = new ContractService(contractRepository.Object, accountRepository.Object, 
                      planRepository.Object, contactRepository.Object, generator.Object);   
  }
  
  
  [TestMethod]
  public void SubmitNewContractTest()
  {
      // Setup Mock Objects
      planRepository
          .Expect(p =&gt; p.GetPlan(1))
          .Returns(new PlanEntity() { Id = 1 });
  
      generator
          .Expect(p =&gt; p.GenerateAccountNumber())
          .Returns("AC0001");
  
      // Not sure what to do here? 
      // How to mock updating the Id field for Inserts?
      //                                                 
      // Creates a correctly populated NewContractRequest instance
      NewContractRequest request = CreateNewContractRequestFullyPopulated();
  
      NewContractResponse response = service.SubmitNewContract(request);
      Assert.IsTrue(response.IsSuccessful);
  }
  </code></pre>
  
  <p>implementation snippet from ContractService class (WCF service contract).</p>
  
  <pre><code>AccountEntity account = new AccountEntity()
  {
      AccountName = request.Contact.Name,
      AccountNumber = accountNumber,
      BillingMethod = BillingMethod.CreditCard,
      IsInvoiceRoot = true,
      BillingAddressType = BillingAddressType.Postal,
      ContactId = request.Contact.Id.Value
  };
  
  accountRepository.InsertAccount(account);
  if (account.Id == null)
  {
      // ERROR
  }
  </code></pre>
  
  <p>I apologise if this information may be a little lacking. I only started learning moq and mocking frameworks today.  ac</p>