Protected members
Unlike interfaces, classes (abstract and non) can have members that can be accessed only by their inheritors. These members are often defined "protected members" as for the visibility flag they are decorated with. When testing components accepting classes with these members, developers might need to configure methods not accessible to them.
Moq supports two ways to configure protected members. Unfortunately, none of them can ensure type safety being based on string and mapping types.
The snippets in this section will be based on the following class
Finally, please note that both approaches require importing of the Moq.Protected
namespace. Also, the helpers targeting protected members are accessible via the Protected()
construct.
Configuring members by name
The Protected
construct offers helpers to configure members via their name.
Parameters can be specified directly or via argument matching: unlike normal methods, developers should use ItExpr
instead of It
.
Unable to rely on static typing, Moq requires explicit typing when configuring properties.
Configuring members using a mapping type
Configuring members by name and without explicit typing can be avoided by instructing Moq to use a mapping type as reference.
The following interface mimics the protected methods exposed by the ServiceBase
class.
Once this type is declared, it's possible to use more comfortable methods to configure the protected methods.
Parameters can be defined using It
as usual.
Also properties are more comfortable to configure when using a mapping type
Last updated