Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3845594
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:16:06+00:00 2026-05-19T16:16:06+00:00

I just started reading on Moq framework and thought of applying it to my

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:16:07+00:00Added an answer on May 19, 2026 at 4:16 pm
    • 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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just learning about escaping things and started reading about how it could
I knw its pretty basic question but I have just started reading it... I
I have just started reading about neural networks and I have a basic question.
I just started reading on Douglas Crockford's Javascript The Good parts where he explains
I just started reading Hacker's Delight and it defines abs(-2 31 ) as -2
I have just started reading Modern C++ Design Generic programming and Design Patterns Applied
I have just started reading about ORMLite since I am interested in using it
Forgive if this question is silly. I just started reading the SugarCRM developer documentation
I just started programming in C# and was reading about dividing your application /
I just started making a database for my website so I am re-reading Database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.