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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Recent TypeMock version simplifies a lot faking certain elements of collections:

Isolate.WhenCalled(() => MyCollection["abc"]).WillReturn("123");
Isolate.WhenCalled(() => MyCollection["def"]).WillReturn("456");

TypeMock memorizes values of collection elements associated with specific index values, so it is now possible to set different expectations on different collection elements.

Unfortunately for method calls it does not work the same way:

// Does not work this way!
Isolate.WhenCalled(() => MyProducts.GetPrice("abc")).WillReturn("123");
Isolate.WhenCalled(() => MyProducts.GetPrice("def")).WillReturn("456");

What would be great if developer could bind expectations with parameters, moreover if he could control what parameter values should be bound.

Like this:
Isolate.WhenCalled((x) => MyProducts.GetPrice(x)).WillReturn("123");
Isolate.WhenCalled(() => MyProducts.GetPrice("abc")).WillReturn("456");

The first statement leaves the argument of GetPrice unbound, i.e. no matter what is sent, GetPrice will return "123". The second statement binds result "456" to explicitly set argument "abc".

Another example, let's say we have a method Add:
int Add(int,int).

Here what we can do to it:

// Sets expectation for specific first and second arguments
Isolate.WhenCalled(() => Calc.Add(1,2)).WillReturn(3);
// Sets expectation for specific second argument
Isolate.WhenCalled<int>((x) => Calc.Add(x,2)).WillReturn(3);
// Sets expectation for any argument
Isolate.WhenCalled<int>((x,y) => Calc.Add(x,y)).WillReturn(3);

Of course right now these examples won't even compile. They will require adding new overloads to Isolate.WhenCalled.

In addition to Isolate.WhenCalled(Action):
Isolate.WhenCalled<T>(Action<T>)
Isolate.WhenCalled<T1>(Action<T1>)
etc...

What do you think about such way of controlling parameterized fakes?
asked by (19.4k points)

1 Answer

0 votes
It's a good idea - in fact setting behavior on overloaded methods and setting behavior according to exact arguments are part of Isolator AAA todo list.

Although I'm not sure if we'll implement this on per argument basis or just setting different expectation on method with different signatures but with the same names.
answered by (11.9k points)
...