Property | Value |
---|---|
Rule ID | PosInfoMoq2011 |
Title | Constructor of the mocked class must be accessible. |
Category | Compilation |
Default severity | Error |
Constructor of the mocked class must be accessible (public
, protected
or internal
)
The mocked class must not contain an inaccessible constructor.
[Fact]
public void Test()
{
var service1 = new Mock<Service>("Hello"); // The constructor invoked is private.
var service2 = new Mock<Service>("Argument 1", 2); // OK
var service3 = new Mock<Service>(MockBehavior.Strict, "Argument 1", 2); // OK
}
public abstract class Service
{
private Service(string a)
{
}
public Service(string a, int b)
{
}
}
To fix a violation of this rule, be sure to call a non-private constructor when instantiate a mocked class.
Do not suppress an error from this rule. If bypassed, the execution of the unit test will be failed with a NotSupportedException
thrown with the “Parent does not have a default constructor. The default constructor must be explicitly defined.” message.