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
Going to admit this straight away that I'm not sure if this is a bug or just a misuse of the framework but I shall outline what I'm seeing and maybe you can advice.
I have a that calls down to another class and I want to test that interaction, here is an example:
public class ClassToTest
{
private readonly IDelegation _delegation;
public ClassToTest(IDelegation delegation)
{
_delegation = delegation;
}
public void MethodToTest(MethodParameters parameters)
{
_delegation.DelegationMethod(parameters);
parameters.InParameter = 10;
}
}
public interface IDelegation
{
void DelegationMethod(MethodParameters parameters);
}
public class MethodParameters
{
public int InParameter { get; set; }
}
Simple enough test:
public class UnitTest1
{
private readonly Mock<IDelegation> _delegationMock;
private readonly ClassToTest _subjectUnderTest;
public UnitTest1()
{
_delegationMock = new Mock<IDelegation>();
_subjectUnderTest = new ClassToTest(_delegationMock.Object);
}
[Fact]
public void ReferenceTesting()
{
_subjectUnderTest.MethodToTest(new MethodParameters {InParameter = 5});
_delegationMock.Verify(delegation =>
delegation.DelegationMethod(It.Is<MethodParameters>(parameters => parameters.InParameter == 5)));
}
}
The issue is, is that this test fails, looking into it (If I split the parameters.InParameter == 5 into a different method) it seems to think that the parameters.InParameter is 10.
I'm guessing when a method gets called it doesn't create a deep copy of the reference type being passed in and is instead capturing the reference, which obviously means that when I change the value in the MethodToTest method it is subsequently changing it in the _delegationMock class?
The text was updated successfully, but these errors were encountered:
Yes, you're right. Moq doesn't create deep copies. (Why would it do that?) Your verification step only sees the final, changed state of the original object.
That's fair, just wanted some confirmation that I wasn't going crazy, I'll close this issue because I see why you wouldn't want to do something like this, thanks 👍
Going to admit this straight away that I'm not sure if this is a bug or just a misuse of the framework but I shall outline what I'm seeing and maybe you can advice.
I have a that calls down to another class and I want to test that interaction, here is an example:
Simple enough test:
The issue is, is that this test fails, looking into it (If I split the
parameters.InParameter == 5
into a different method) it seems to think that theparameters.InParameter
is 10.I'm guessing when a method gets called it doesn't create a deep copy of the reference type being passed in and is instead capturing the reference, which obviously means that when I change the value in the
MethodToTest
method it is subsequently changing it in the_delegationMock
class?The text was updated successfully, but these errors were encountered: