chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I'm in the process of evaluating typemock. Here is my question


util.GetConfigItem("DontMock", ref historyFromDaysAgo);
util.GetConfigItem("Mock", ref historyBeginHours);
util.GetConfigItem("DontMock1", ref historyEndHours);
util.GetConfigItem("Whatever1", ref extendedHours2Show);
util.GetConfigItem("Mock1", ref historyBeginHours);
util.GetConfigItem("whatever2", ref extendedHours2Show);

Util is a static class, and I only want to fake this method when the parameter is Mock or Mock1. I tried something like this

int configIntVal = 0;
Isolate.WhenCalled(() => util.GetConfigItem("Mock", ref configIntVal)).WithExactArguments().DoInstead((callContext) =>
{
int? retVal = callContext.Parameters[1] as int?;
string configItem = callContext.Parameters[0] as string;
callContext.Parameters[1] = 2;
});






it will call this fake method all the time, what is the best way to handle something like this
asked by ben1628 (2k points)

3 Answers

0 votes
Hi Ben, and welcome to Typemock.

What you need is to set conditional behavior - you can see more in the developer's guide under the c# API (direct link: https://www.typemock.com/Docs/UserGuide/ ... tsAAA.html).

You should end up with something like:
Isolate.WhenCalled((string s) => util.GetConfigItem(s, ref configIntVal)).
    AndArgumentsMatch(s => s == "Mock" || s == "Mock2").
    WillReturn(fakeValue)


Please let me know if this helps, or if you need any further help with your evaluation.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hmmm, I am not that far off. This was one of the things I tried, and it was basically the same as what you suggested. However, it didn't seem to work for me.

I'll tried again and let you know.
answered by ben1628 (2k points)
0 votes
okay, I figure out what my problem is

I had

Isolate.WhenCalled((string s) => util.GetConfigItem("Mock", ref configIntVal))
.....

and I ran into parameter out of range error, and so I didn't go further than that.


The following code is working fine:

Isolate.WhenCalled((string s) => util.GetConfigItem(s, ref configIntVal))
.AndArgumentsMatch((s) => s == "Mock" || s == "Mock1")
.DoInstead((callContext) =>
{
callContext.Parameters[1] = 2;
});


it set the ref to the correct value and bypass everything else that are not Mock or Mock1.


Thanks for your help.
answered by ben1628 (2k points)
...