Verify()
method must specify the Times
argumentProperty | Value |
---|---|
Rule ID | PosInfoMoq1007 |
Title | The Verify() method must specify the Times argument |
Category | Design |
Default severity | Warning |
When calling the Verify()
method, if the Times
argument is not specified, Moq will assume Times.AtLeastOnce()
by default.
Although Moq provides a default behavior (Times.AtLeastOnce()
), relying on it can make test intentions unclear or ambiguous.
Specifying the Times
argument explicitly improves readability and ensures that test failures are interpreted correctly.
For example, the following code omits the Times
argument:
[Fact]
public void SaveUser_ShouldBeCalled()
{
var repository = new Mock<IRepository>();
var service = new UserService(repository.Object);
service.Save(new User());
// Ambiguous: defaults internally to Times.AtLeastOnce()
repository.Verify(x => x.Save(It.IsAny<User>()));
}
This code works the same as:
repository.Verify(x => x.Save(It.IsAny<User>()), Times.AtLeastOnce());
However, omitting the Times
argument makes it less obvious to a reader what the expected behavior is.
Instead, you should write the Times
explicitly:
[Fact]
public void SaveUser_ShouldBeCalledOnce()
{
var repository = new Mock<IRepository>();
var service = new UserService(repository.Object);
service.Save(new User());
// Clear and explicit
repository.Verify(x => x.Save(It.IsAny<User>()), Times.Once());
}
To fix a violation of this rule, always specify the Times
argument explicitly when calling Verify()
method.
Examples:
Times.Once()
Times.Never()
Times.AtLeast(2)
Times.Exactly(3)
You may suppress warnings from this rule if you are fine with the implicit default of Times.AtLeastOnce()
.
However, for readability and maintainability, it is strongly recommended to always be explicit with the Times
argument.