0

I'd like to mock a dependent method of the method I wish to test. How can I accomplish this using Moq?

public class ClassA : IClassA
{


public bool Method1(string entityType, string entityIdString)
{
    int entityId;

    if (!Int32.TryParse(entityIdString, out entityId))
    {
        return false;
    }

    return this.Method2() && _agent.Id == entityId;
}

public bool Method2() { ... }

}

How do I mock the sibling Method2() in my unit test?

This article didn't help me, because there is no inheritence in my target class. http://www.codenutz.com/unit-testing-mocking-base-class-methods-with-moq/

Michael R
  • 1,547
  • 1
  • 19
  • 27
  • 1
    [With Moq](http://stackoverflow.com/q/2462602/126014) [you can't](http://stackoverflow.com/q/1972831/126014). – Mark Seemann Aug 15 '15 at 07:00
  • You can mock the ```Method2()``` when this method is marked as ```virtual```. Then you can create the new instance of Mock like this ```Mock cut = new Mock() { CallBase = true };```. And set-up the ```Method2``` like you normaly would ```cut.Setup(c => c.Method2()).Returns(true);```. Finally call the tested method ```bool result = cut.Object.Method1("entityType", "1");```. – Daniel Dušek Aug 15 '15 at 10:56

0 Answers0