This is a migrated thread and some comments may be shown as answers.

Mocking Action<>

3 Answers 91 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jay
Top achievements
Rank 1
Jay asked on 20 Jun 2012, 07:28 PM
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");
    }
}

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 25 Jun 2012, 02:56 PM
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 >>

0
Jay
Top achievements
Rank 1
answered on 25 Jun 2012, 06:40 PM
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
0
Ricky
Telerik team
answered on 29 Jun 2012, 12:16 PM
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 >>

Tags
General Discussions
Asked by
Jay
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Jay
Top achievements
Rank 1
Share this question
or