Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MOQ - How to verify static class call and delegates?

Tags:

c#

c#-4.0

moq

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);
    }
}
like image 348
stackoverflowuser Avatar asked Dec 14 '25 08:12

stackoverflowuser


1 Answers

  • 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);
like image 195
Aliostad Avatar answered Dec 16 '25 21:12

Aliostad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!