How to set Arguments when mocking a "param" param function?

5 posts, 1 answers
  1. Christiano
    Christiano avatar
    17 posts
    Member since:
    Dec 2008

    Posted 31 Aug 2012 Link to this post

    Hi there!

    Let's suppose I have a very simple class like this:

    public class SimpleClass
    {
    public string GetFormatedString(string mask, params object[] par) 
    {
    return string.Format(mask, par);
    }
    }

    I honestly have tried to Mock this method considering the fact that it may be called with an unpredictable number of arguments on "par" when running the tests. But I was unable to find something like this on JustMock documentation. I found many examples where you have to set exactly the number of arguments passed. Also mocking the second parameter as Args.IsAny<object[]>() doesnt work.

    Can anyone help me on this question, please?
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 04 Sep 2012 Link to this post

    Hi Christiano,
    Thanks again for contacting us.

    I wrote the test in the following way and it is working as expected.

    var simpleClass = Mock.Create<SimpleClass>();
     
    var expected ="expected";
     
    Mock.Arrange(() => simpleClass.GetMessage("test", Arg.IsAny<object>())).Returns(() => expected);
     
    Assert.AreEqual(expected, simpleClass.GetMessage("test", new Bar()));

    As params argument can be passed separately, it is mocked the same way. The good thing is that you can mock a method based on different argument combination and you know for which it should your hit your mock instance.

    This is something similar to other popular mocking tools like Moq / Rhino and in this way we adopt the best practices out there.
                      

    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft R3 2016 release webinar banner
  4. Christiano
    Christiano avatar
    17 posts
    Member since:
    Dec 2008

    Posted 06 Sep 2012 Link to this post

    Thank you for the answer, but I believe that I wasnt clear enough about the problem. My mistake, and I'm sorry about that. Maybe,a piece of code can explain it better:

    [TestMethod]
    public void TestMethod1()
    {
      string expected = "crap";
      string tmp = string.Empty;
      SimpleLib.SimpleClass simple = Mock.Create<SimpleClass>();
     
      Mock.Arrange(() => simple.GetFormatedString(Arg.AnyString, Arg.IsAny<object>())).Returns(() => expected);
      tmp = simple.GetFormatedString("classe", 0);
      Assert.AreEqual( 0, string.Compare(tmp,expected)); //OK!
     
      tmp = simple.GetFormatedString("classe", 0,0);
      Assert.AreEqual(0, string.Compare(tmp, expected)); //NO!!!
     
      tmp = simple.GetFormatedString("classe", 0, 0, 0);
      Assert.AreEqual(0, string.Compare(tmp, expected)); //NO!!
    }

    As you can see, I have to arrange each call with a different number of parameters. And this is exactly what I want to avoid, because internally, this method can be invoked with an unpredictable number of arguments. Is it possible to define in a single Mock.Arrange any call?

    Thank you!
  5. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 12 Sep 2012 Link to this post

    Hi Christiano,

    Thanks again for contacting us. It is also possible to write the test in the following way:

    [TestMethod]
    public void TestMethod1()
    {
        string expected = "crap";
        string tmp = string.Empty;
        SimpleClass simple = Mock.Create<SimpleClass>();
     
        Mock.Arrange(() => simple.GetFormatedString(string.Empty)).IgnoreArguments().Returns(() => expected);
        tmp = simple.GetFormatedString("classe", 0);
        Assert.AreEqual(0, string.Compare(tmp, expected)); //OK!
     
        tmp = simple.GetFormatedString("classe", 0, 0);
        Assert.AreEqual(0, string.Compare(tmp, expected)); //OK!!!
     
        tmp = simple.GetFormatedString("classe", 0, 0, 0);
        Assert.AreEqual(0, string.Compare(tmp, expected)); //OK!!
    }

    In this way , you don't have to specify each param separately.


    Kind Regards
    Ricky
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

  6. Christiano
    Christiano avatar
    17 posts
    Member since:
    Dec 2008

    Posted 12 Sep 2012 Link to this post

    This is what I was looking for. Thank you!
Back to Top
DevCraft R3 2016 release webinar banner