Does Moq support 'in' parameters?

Viewed 85

I am trying to create a mock for an interface which has an 'in' parameter. It's not working as I hoped. Here's a simplified example:

namespace MoqExample.Test
{
    public interface IPublisher
    {
        void Publish(in int i);
    }

    public class Tests
    {
        private Mock<IPublisher> publisher;

        [SetUp]
        public void BeforeEachTest()
        {
            publisher = new Mock<IPublisher>(MockBehavior.Strict);
            publisher.Setup(publisher => publisher.Publish(It.IsAny<int>()));
        }

        [Test]
        public void TestPublishInt()
        {
            publisher.Object.Publish(1);
        }
    }
}

This example gives this error:

  Message: 
    Moq.MockException : IPublisher.Publish(1) invocation failed with mock behavior Strict.
    All invocations on the mock must have a corresponding setup.
  Stack Trace: 
    FailForStrictMock.Handle(Invocation invocation, Mock mock)
    IInterceptor.Intercept(Invocation invocation)
    Interceptor.Intercept(IInvocation underlying)
    AbstractInvocation.Proceed()
    IPublisherProxy.Publish(Int32& i)
    Tests.TestPublishInt() line 26

If I remove 'in' from the interface the test passes. But I don't have that choice in my use case.

Is this supported?

1 Answers

Looking at the documentation, first sentence:

The in keyword causes arguments to be passed by reference.

Try using:

publisher.Setup(publisher => publisher.Publish(It.Ref<int>.IsAny));
Related