Setup()
method must be used only on overridable membersProperty | Value |
---|---|
Rule ID | PosInfoMoq2001 |
Title | The Setup() method must be used only on overridable members |
Category | Compilation |
Default severity | Error |
The Setup()
method must be applied only for overridable members.
An overridable member is a methode or property which is in:
interface
.sealed
class
. In this case, the member must be:
abstract
.virtual
The Setup()
method must be applied only for overridable members.
For example, the following methods and properties can be mock and used in the Setup()
method:
IService.MethodCanBeMocked()
IService.PropertyCanBeMocked
Service.VirtualMethodCanBeMocked
Service.VirtualPropertyCanBeMocked
Service.AbstractMethodCanBeMocked
Service.AbstractPropertyCanBeMocked
public interface IService
{
void MethodCanBeMocked();
string PropertyCanBeMocked { get; set; }
}
public abstract class Service
{
public virtual void VirtualMethodCanBeMocked() { ... }
public virtual void VirtualPropertyCanBeMocked() { ... }
public abstract void AbstractMethodCanBeMocked();
public abstract void AbstractPropertyCanBeMocked();
}
NOTE: The extension methods can not be overriden. The C# syntax looks like a member method an interface or class, but the extension method are just simple static methods which can not be overriden.
To fix a violation of this rule, be sure to mock a member in the Setup()
method which can be overriden.
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 “Extensions methods may not be used in setup/verification expressions” message.