Reputation: 29
I was wondering if there was a way/workaround to be able to capture the output from an Arrange call in JustMock.
e.g. this is the kind of thing that I would like to do:
Mock.Arrange(() => foo.Bar()).CallOriginal(result => { /* stuff I want to do with the result */ });
I have actually done something similar already to capture parameters, but as I said, I haven't been able to figure out how to capture the return value. Here is how to capture the parameters though:
string myVar;
void Setup() {
Mock.Arrange(() => foo.Bar(Arg.Matches<string>(val => Capture(val))).CallOriginal();
}
private boolean Capture(string val) {
myVar = val;
// any other stuff
return true;
}
EDIT: found a (ridiculous) solution. Leaving this question in case someone else needs it, or if anyone else finds a better way.
bool callOriginal = false;
string myVar;
string myRes;
void Setup() {
Mock.Arrange(() => foo.Bar(Arg.Matches<string>(val => Capture(val))).Returns(() => CallOriginalMethod());
Mock.Arrange(() => foo.Bar(Arg.Matches<string> val => CallOriginal()
}
private boolean Capture(string val) {
if (callOriginal) return false;
myVar = val;
// any other stuff
return true;
}
private boolean CallOriginal() {
return callOriginal;
}
private string CallOriginalMethod() {
callOriginal = true;
var res = foo.Bar(myVar); //here we get the return value
myRes = res;
callOriginal = false;
return res;
}
This will not work for multithreaded applications, but it works for what I need.
Upvotes: 1
Views: 119