title: Moq - Reference parameter in virtual method doesn't update correctly when class is mocked
body: <p>I have test that check ref parameter value from event that raised after the change. The test work fine when I create real class but when I try mocked class its fail. Simple code example:</p>
  
  <pre><code>[TestClass]
  public class Fixture
  {
      [TestMethod]
      public void CheckRealClass()
      {
          // this test passed
          var classToTest = new NumberChangerClass();
          CheckClass(classToTest);
      }
  
      [TestMethod]
      public void CheckMockedClass()
      {
          // this test failed
          var classToTest = new Mock&lt;NumberChangerClass&gt;() { CallBase = true };
          CheckClass(classToTest.Object);
      }
  
      private void CheckClass(NumberChangerClass numberChangerClass)
      {
          // Arrange
          var number = 1;
          numberChangerClass.StorageChanged += (sender, args) =&gt;
          {
              Assert.AreEqual(2, number);
          };
  
  
          // Act
          numberChangerClass.SetValue(ref number, 2);
      }
  }
  
  public class NumberChangerClass
  {
      public event EventHandler StorageChanged;
  
      public virtual void SetValue(ref int storage, int newValue)
      {
          storage = newValue;
          StorageChanged?.Invoke(this, null);
      }
  }
  </code></pre>
  
  <p>In <code>CheckMockedClass</code> the <code>number</code> is actually changed but only after the the code exits from <code>SetValue</code> method.</p>
  
  <p>I use Moq version 4.2.1409.1722 but it doesn't work also in the latest version (4.8.1).</p>
  
  <p>There is a way to make <code>CheckMockedClass</code> to pass?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I've opened issue in moq github: <a href="https://github.com/moq/moq4/issues/585" rel="nofollow noreferrer">link</a></p>
  
  <p>short answer: there nothing that can be done about it.</p>
  
  <p>more details:</p>
  
  <blockquote>
    <p>the behaviour you are observing is a direct consequence of how DynamicProxy (which Moq depends on) handles by-reference parameters during method call interception (which is documented at <a href="https://github.com/castleproject/Core/blob/master/docs/dynamicproxy-by-ref-parameters.md" rel="nofollow noreferrer">https://github.com/castleproject/Core/blob/master/docs/dynamicproxy-by-ref-parameters.md</a>): Arguments are buffered in a separate location during call interception and only written back at the end of interception, that's why refparameters are updated in a delayed / deferred manner. There is unfortunately nothing that can be done about it.</p>
  </blockquote>
  
  <p>I still try to find a way to get the updated number when I check <code>Assert.AreEqual(2, number);</code></p>