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
When using MOQ I have used the following to transform some input parameter in a mocked service, basically I us a replace transform on a name to generate a value

var mock = new Mock<IFieldLookupProvider>();
mock.Setup(f => f.LookupWorkItemFieldValue(It.IsAny<string>())).Returns((string input) => input.Replace(".", "_"));

Is it possible to do something similar in Typemock? I can't work out how to us an input parameter in the WillReturn block - Is it even possible

var mock = Isolate.Fake.Instance<IFieldLookupProvider>();
Isolate.WhenCalled((string input) => mock.LookupWorkItemFieldValue(input))
.AndArgumentsMatch(input => string.IsNullOrEmpty(input) == false)
.WillReturn(input.Replace(".", "|"));

You get a syntax error in the final block as the variable input is unknown
asked by rfennell (6.8k points)

2 Answers

0 votes
Hi Richard,

I believe that this is what you were looking for:

[TestMethod]
public void TestMethod1()
{
    var mock = Isolate.Fake.Instance<IFieldLookupProvider>();
    Isolate.WhenCalled((string input) => mock.LookupWorkItemFieldValue(input))
           .AndArgumentsMatch(input => string.IsNullOrEmpty(input) == false)
           .DoInstead(context =>
              {
                    string  input = context.Parameters[0] as string;
                    input.Replace(".", "|");
              });
}


You can retrieve the input parameters from the context of the call and use DoInstead to write custom logic.

Please let me know if it helps.
answered by alex (17k points)
0 votes
Thanks it was the context.Parameters[0] I was missing
answered by rfennell (6.8k points)
...