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've got a function that returns a value that is computed by several private functions in the same class (obviously). I'd like to mock the return values of the private functions so that they don't get called for real.

FirstlyHow might I use Reflective Mocks to do this? The obvious way:
            Mock m = MockManager.Mock(typeof(MyClass));
            m.ExpectAndReturn("PrivateFunction", host);
            m.ExpectGet("PrivateProperty", port);

           MyClass g = new MyClass();

results in a call to "MyClass.Foo()", which kills the test because it throws an exception under these conditions (which is why I want to Mock it).

Secondly, How might I use Natural Mocks to do this? If I do this:
            MyClass g = new MyClass();
            using (RecordExpectations r = RecorderManager.StartRecording())

it won't compile because PrivateFunction is a private function. If, instead of "g" I use a private accessor, TypeMock looks for the private accessor type, not the MyClass type (at least according to the Trace).

Thirdly, Is there a way to use private accessors to Mock the accessed-class's private members?

Ultimate fighter
asked by erichorne (3.4k points)

2 Answers

0 votes

You're on the right track 8)
Reflective mocks should be used to Mock private methods.

In your case, there could be a few reasons why the test fails:
1. You are mocking the wrong instance - Try using MockAll
2. You are not mocking the correct method (Are you mocking "Foo"?).

Please use the Tracer to find out what is happening behind the scenes. Send us the exported trace if you want us to pin point exactly what is happening.

As for you last question, this is not currently available but is a nice idea to use for Natural Mocks. I am not sure how TypeMock will map the accessor to the real type, but I will add this to our feature list.
answered by scott (32k points)
0 votes
MockAll did it for me.


herbalaire vaporizer
answered by erichorne (3.4k points)