3

I just started reading on Moq framework and thought of applying it to my existing code. I could come up with a test case to verify basic function calls. But now stuck at applying it to delegates and static classes.

Below is the system under test (SUT)

public class SUT : ISUT
{
   private IInterface1 i1;,
   private IInterface2 i2;

   public SUT(IInterface1 i1, IInterface2 i2)
   { 
      this.i1 = i1;
      this.i2 = i2;
   }

   public void FunctionToTest(Action<string> someDelegate, Action otherDelegate)
   {
       var list = i2.GetList();

       foreach(var r in list)
       {
      i1.SomeFunction(r);

          if(someDelegate != null)
              someDelegate("message");                        

          SomeHelper.LogData(r);
       }   

       if(otherDelegate != null)    
             otherDelegate();
   }
}

I have setup my test as below

[TestFixture]
public class when_list_contains_atleast_one_item
{
   ISUT sut;
   Mock<IInterface1> mockI1;
   Mock<IInterface2> mockI2;

   public SUT_Tester()
   {
       mockI1 = new Mock<IInterface1>();
       mockI2 = new Mock<IInterface2>();
       sut = new SUT(mockI1.Object, mockI2.Object);
   }

   [Test]
   public void it_should_call_somefunction_calldelegates_and_log_data()
   {
       var dummyList = new List<string> { "string1", "string2" };
       mockI2.Setup(m2 => m2.GetList()).Returns(dummyList).Verifiable();

       sut.FunctionToTest(It.IsAny<Action<string>>(), It.IsAny<Action>());   

       // How to verify delegates were raised
       // How to verify SomeHelper.LogData was called                

       mockI1.Verify(m => m.SomeFunction(It.IsAny<string>()), Times.Exactly(2));
       mockI2.Verify();
   }
}

How to verify that someDelegate and otherDelegate were raised ? SomeHelper is a static class and LogData is a static function. How to verify that LogData was called?

Below is the SomeHelper class

public static class SomeHelper
{
    static ILogger logger = LoggerManager.GetLogger("Something");
    public static void LogData(string input)
    {
        logger.Info(input);
    }
}
stackoverflowuser
  • 22,212
  • 29
  • 67
  • 92

2 Answers2

4
  • You cannot verify static methods since they cannot be mocked by Moq.
  • In order to verify calls on the delegates, create them so that they call a local function and you keep the state and verify:

    string localString = string.Empty;

    Action<string> action = (string s) => localString = s;

    // ... pass it to the test

    Assert.(localString == "TheStringItMustbe");

(UPDATE)

OK to check an action that has no in params, increment a local variable and assert if it has been incremented (or something similar):

int localVar = 0;
Action action = () => localVar++;
// ... pass it to the test
Assert.(localVar == 1);
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • excuse my ignorance. But how to verify the Action delegate (that does not take string)? – stackoverflowuser Jan 27 '11 at 19:56
  • Hi sorry, just got back home. If you have an action delegate with no parameters, have a local variable and increment it in there and check if it has been incremented. See code sample in y update. – Aliostad Jan 27 '11 at 23:35
1

You might want to consider changing the IInterface1/2 interfaces so that they either take an ILogger as a parameter into SomeFunction, or have a settable ILogger property.

Then you could pass a mocked ILogger into SomeFunction.

micahtan
  • 18,530
  • 1
  • 38
  • 33