I'm a newbie in NSubstitue mocking framework.
I have the following requirements:
- Match
DoSomethingparameter. - Do something (add one of the parameter's fields to
myList) - Return a specific value.
The code should look like this:
mockDependency.When(x => x.DoSomething(Arg.Is<string>(s => s == "specificParameter")))
.Do(x => myList.Add(x.Name))
.Return("ReturnValue");
The only way I know to meet the requirements above is to do something like this:
mockDependency.When(x => x.DoSomething(Arg.Is<string>(s => s == "specificParameter")).Do(x => myList.Add(x.Name))
Then, configure the return value separately:
mockDependency.DoSomething(Arg.Is<string>(s => s == "specificParameter")).Return(myValue)
Is there a simpler way to achieve the requirements?
I know that in fakeItEasy I can do something like this:
A.CallTo(() => mockDependency.DoSomething(A<string> .That.Matches(x=> x == "specificParameter") )).Invokes(x => myList.Add(x.Name)).Returns(myValue)
You can combine returning a value via
Returnswith doing an action usingAndDoes.The
stringvalue passed toDoSomething(here: specificParameter) acts as the filter; only whenDoSomethinggets called with that value, the specified setup of the return value and action will be invoked.