
Christiano
Top achievements
Rank 2
Christiano
asked on 01 Sep 2012, 02:34 AM
Hi there!
Let's suppose I have a very simple class like this:
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?
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?
4 Answers, 1 is accepted
0
Hi Christiano,
Thanks again for contacting us.
I wrote the test in the following way and it is working as expected.
Ricky
the Telerik team
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.
Ricky
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
0

Christiano
Top achievements
Rank 2
answered on 07 Sep 2012, 01:32 AM
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:
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!
[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!
0
Accepted
Hi Christiano,
In this way , you don't have to specify each param separately.
Kind Regards
Ricky
the Telerik team
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.
0

Christiano
Top achievements
Rank 2
answered on 12 Sep 2012, 04:36 PM
This is what I was looking for. Thank you!