You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
[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<NumberChangerClass>() { CallBase = true };
CheckClass(classToTest.Object);
}
private void CheckClass(NumberChangerClass numberChangerClass)
{
// Arrange
var number = 1;
numberChangerClass.StorageChanged += (sender, args) =>
{
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);
}
}
In CheckMockedClass the number is actually changed but only after the the code exits from SetValue method.
Thank you for the help.
The text was updated successfully, but these errors were encountered:
@iitaiy, 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 https://github.com/castleproject/Core/blob/master/docs/dynamicproxy-by-ref-parameters.md): 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.
Thank you for taking the time to report this, though!
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:
In
CheckMockedClass
thenumber
is actually changed but only after the the code exits fromSetValue
method.Thank you for the help.
The text was updated successfully, but these errors were encountered: