Moq 是否支持 'in' 参数?
Does Moq support 'in' parameters?
我正在尝试为具有 'in' 参数的接口创建模拟。它没有像我希望的那样工作。这是一个简化的例子:
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);
}
}
}
这个例子给出了这个错误:
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
如果我从界面中删除 'in',则测试通过。但是在我的用例中我没有那个选择。
支持吗?
看documentation,第一句:
The in keyword causes arguments to be passed by reference.
尝试使用:
publisher.Setup(publisher => publisher.Publish(It.Ref<int>.IsAny));
我正在尝试为具有 'in' 参数的接口创建模拟。它没有像我希望的那样工作。这是一个简化的例子:
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);
}
}
}
这个例子给出了这个错误:
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
如果我从界面中删除 'in',则测试通过。但是在我的用例中我没有那个选择。
支持吗?
看documentation,第一句:
The in keyword causes arguments to be passed by reference.
尝试使用:
publisher.Setup(publisher => publisher.Publish(It.Ref<int>.IsAny));