Property | Value |
---|---|
Rule ID | PosInfoMoq2007 |
Title | The As<T>() method can be used only with interfaces. |
Category | Compilation |
Default severity | Error |
The As<T>()
method is used with a type which is not an interface.
Moq allows to add additional implementations for mocked class (or interface) by adding additional interfaces
with the As<T>()
method.
[Fact]
public void Test()
{
var service = new Mock<Service>();
service.As<IDisposable>() // Add IDisposable implementation for the mocked Service class.
.Setup(s => s.Dispose());
service.As<OtherService>(); // An error will be raised, because we can't mock additional implementation of a class.
}
public abstract class Service
{
}
public abstract class OtherService
{
}
To fix a violation of this rule, use an interface when using the As<T>()
method.
Do not suppress an error from this rule. If bypassed, the execution of the unit test will be failed with a MoqException
thrown with the "Can only add interfaces to the mock." message.