简体   繁体   中英

Rhino Mock: Mocking private method calls c#

It is possible to mock out a priavte method call.

Example

   Public void StartProcess(string valueFound)
    {
        if(this.value == valuefound)
        {
            MockThisPriavteMethodCall();
        }

        AnotherPrivateMethodCallIDontNotWantToMockOut();
    }

This situation is that I want to test one private method but I want to mock the result of the other.

In the above example I want to be able to mock the method call "MockThisPriavteMethodCall()", but I want the second "AnotherPrivateMethodCallIDontNotWantToMockOut()", to run.

Can I use some form of expect?

Can this be done in Rhino Mock, is this the correct thinking?

Addition: I am trying to test code that is in aa ViewModel class.

The example above is too show that it is one of a few public methods. These methods call in into many different private methods. Which again calls into many private methods.

Should I be testing certain paths and situations allowing the code to flow through a few branches or should I be only testing methods by them self's?

Mocking is used from the object under test to a dependency - at the point where you want to mock out parts of the code in the very same object that you're testing against, something isn't right.

It's hard to tell too much from your pseudo-code, but it sounds like you potentially want to introduce a new dependency for the code that you want to fake out. If you can give us more concrete details of the context, it would really help.

There's really not a clean way to do this outright.

You can do it by changing the method call to protected and creating a testable class that inherits from your class and creating an expectation for it out that way.

That having been said, Jon Skeet is correct , you may be using Mocking wrongly here. You clearly want to return a dummy value from a method, and once you make the method protected you can do that as follows:

myclass.Stub(s => s.MockThisMethodCall()).Return("WhateverValueYouExpect"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM