Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It.Is reference types #621

Closed
MarkDaviesEsendex opened this issue May 22, 2018 · 2 comments
Closed

It.Is reference types #621

MarkDaviesEsendex opened this issue May 22, 2018 · 2 comments

Comments

@MarkDaviesEsendex
Copy link

MarkDaviesEsendex commented May 22, 2018

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?

@stakx
Copy link
Contributor

stakx commented May 22, 2018

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.

@MarkDaviesEsendex
Copy link
Author

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants