chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I have an abstract class with a protected method that I want to mock. How do I accomplish this? Thanks. :D

Example: How do I mock Method() ?

abstract public class BaseClass {
  protected void Method() { }
}

public class SubClass:BaseClass {

  public void Create() {
    Method();
  }  
}
asked by (640 points)

3 Answers

0 votes
After much sifting and reading, I realized that it doesn't matter it is a protected method if I use Reflective Mocks.

Mock mock = MockManager.Mock(typeof(SubClass));
mock.ExpectCall("Method");


However, I don't believe I can do the same using NaturalMocks, can anyone verify that?
answered by (640 points)
0 votes
Hi,

Basically, you're correct. Because the members you are trying to mock are internal, the Natural code won't even compile, and so you'll need to use reflective.

Unless...

There are two ways that allow you to work with Natural Mocks. One is using private accessors that VS can generate. These accessors are public and you can mock them.

The second way is to use InternalsVisibleTo attribute on the tested assembly, to expose the internals to the testing assembly. Once exposed they behave as public members, and therefore applicable in Natural.

Both are viable options.

Please click the following link for more information on mocking protected method


Good luck,
answered by (14.5k points)
0 votes
Unfortunately, Private Accessors are being deprecated and are no longer recommended by Microsoft.
answered by (8.4k points)
...