Reputation: 153
I would like to verify that a method is only called once.
mock.Verify(x => x.Method("String", out It.IsAny<StringBuilder>()), Times.Once);
I don't care about the second parameter, it could be anything.
I get the following error message because of the out
parameter:
'out' argument must be an assignable variable, field or array element
Upvotes: 8
Views: 5100
Reputation: 1032
You can verify for Any by using It.Ref<StringBuilder>.IsAny
. In your case the solution would be the following:
mock.Verify(x => x.Method("String", out It.Ref<StringBuilder>.IsAny), Times.Once);
This function was added in Moq 4.8.0-rc1
Upvotes: 3
Reputation: 247088
Try following the error message instructions and putting the out parameter in a variable.
var builder = new StringBuilder();
mock.Verify(x => x.Method("String", out builder), Times.Once);
Here is a full example that passes when tested.
[TestClass]
public class ExampleTest {
public interface IInterface {
void Method(string p, out StringBuilder builder);
}
public class MyClass {
private IInterface p;
public MyClass(IInterface p) {
this.p = p;
}
public void Act() {
var builder = new StringBuilder();
p.Method("String", out builder);
}
}
[TestMethod]
public void Should_Ignore_Out() {
//Arrange
var mock = new Mock<IInterface>();
var sut = new MyClass(mock.Object);
//Act
sut.Act();
//Assert
var builder = new StringBuilder();
mock.Verify(x => x.Method("String", out builder), Times.Once);
}
}
Upvotes: 1