Mocking Action<>

4 posts, 0 answers
  1. Jay
    Jay avatar
    5 posts
    Member since:
    Feb 2008

    Posted 20 Jun 2012 Link to this post

    I am trying to Mock a method that Action<T> delegate. The goal is to mock the Action.Invoke and Assert if the correct value get sent back , but the following code does not seem to work. Any pointers ?

    Jay


    public interface IClientServerMockingTest
    {
        void CallMethod(string id, Action<string> status);
    }

    [TestClass]
    public class ClientServerMockingTest : IClientServerMockingTest
    {
        [TestMethod]
        public void TestMethod1()
        {
            var clientServer = Mock.Create<ClientServerMockingTest>();
             var action = Mock.Create<Action<string>>();
             Mock.Arrange(() => clientServer.CallMethod("demo", action)).Raises(() => clientServer.CallMethod("demo", action));
            clientServer.CallMethod("demo", action);    
            Mock.Assert(clientServer);
        }
     
        public void CallMethod(string id, Action<string> action)
        {
            action.Invoke("worked");
        }
    }
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 25 Jun 2012 Link to this post

    Hi Jay,

    Thanks again for bringing up the question. However, here are few issues that I found with your test:

    1. You are trying to mock a test class itself, which is not an ideal scenario for mocking . Generally, it should be a target class or libraries inside test class.
    2. You are creating Mock of an Action delegate which is invalid and you can always write the following:
    Mock.Arrange(() => clientServer.CallMethod("demo", Arg.IsAny<Action<string>>()))

    Moreover the following line has a recursion, which is telling clientServer.CallMethod to do a self-invocation which can eventually crash the test.

    Hope this gives you some sort of pointers.

    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
  4. Jay
    Jay avatar
    5 posts
    Member since:
    Feb 2008

    Posted 25 Jun 2012 Link to this post

    Thanks for this, here is how I have got the code so far

    [TestClass]
    public class TestClass
    {
        [TestMethod]
        public void TestMethod1()
        {
            var clientServer = Mock.Create<ClientServerMockingTest>();
             
     
            Mock.Arrange(() => clientServer.CallMethod("demo", Arg.IsAny<Action<string>>()));
     
     
            Action<string> action = Console.WriteLine;
            clientServer.CallMethod("demo", action);    
     
            Mock.Assert(clientServer);
        }
    }
     
    public interface IClientServerMockingTest
    {
        void CallMethod(string id, Action<string> status);
    }
     
    public class ClientServerMockingTest : IClientServerMockingTest
    {
        public void CallMethod(string id, Action<string> status)
        {
            status.Invoke("Worked");
        }
    }



    what I want to do is Mock the status.invoke so for example instead of the messaged "worked" it will return some thing else we pass.

    Thanks

    Jay
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 29 Jun 2012 Link to this post

    Hi Jay,

    Thanks again for contacting us. You can write the test using DoInstead that will ensure that Action<string> is invoked as you have mentioned it in Mock.Arrange

    [TestMethod]
          public void TestMethod1()
          {
              var clientServer = Mock.Create<ClientServerMockingTest>();
     
              Mock.Arrange(() => clientServer.CallMethod("demo", Arg.IsAny<Action<string>>())).DoInstead((string id, Action<string> aciton) =>
              {
                  aciton.Invoke("faked");
              });
     
              Action<string> action = Console.WriteLine;
     
              clientServer.CallMethod("demo", action);
     
              Mock.Assert(clientServer);
          }


    Kind Regards
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top