-
-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stub protected method with out/ref params #223
Comments
Hmm, this is tricky. Let me just throw out an idea here. Given that one of Moq's design goals is to provide a strongly-typed API (and // Define a delegate that has the exact same signature
// as the protected method that you want to set up in a mock:
delegate void ImProtectedWithOutAction(out int result);
…
var mock = new Mock<Thing>();
// Then, using a (currently non-existent) generic overload of `Setup`, let Moq
// know what signature to expect from the protected method:
int result;
mock.Protected().Setup<ImProtectedWithOutAction>("ImProtectedWithOut",
method => method.Invoke(out result)); Different idea, but conceptually similar: // Define an interface that describes all protected members of Thing you want to set up:
interface ProtectedMembersOfThing
{
void ImProtectedWithOut(out int result);
}
…
int result;
mock.Protected<ProtectedMembersOfThing>().Setup(thing => thing.ImProtectedWithOut(out result)); This would be preferable to idea (1) above if there are lots of protected members you want to set up. But it feels a little like an abuse of |
Visiting for triage: Let's look at this again once #105 has been implemented. |
You'll be able to do this with Moq 4.8. Let's say you have e.g. this type that you'd like to mock: public abstract class Foo
{
public bool TryFiddle(string intact, out string fiddled)
{
return this.TryFiddleImpl(intact, out fiddled);
}
protected abstract bool TryFiddleImpl(string intact, out string fiddled);
} To set up // type you declare to get at the protected member:
public interface FooImpl
{
bool TryFiddleImpl(string intact, out string fiddled);
}
// delegate type you declare so you can setup a `.Returns` callback
// that can access the out parameter:
public delegate bool TryFiddleImplReturns(string intact, out string fiddled); Then finally, you can put everything together e.g. as follows: var mock = new Mock<Foo>();
string fiddled;
mock.Protected().As<FooImpl>()
.Setup(m => m.TryFiddleImpl(It.IsAny<string>(), out fiddled))
.Returns(new TryFiddleImplReturns((string i, out string o) =>
{
o = "(" + (i ?? "") + ")";
return i != null;
}));
if (mock.Object.TryFiddle("hello", out fiddled))
{
Console.WriteLine(fiddled);
} Admittedly, that's quite some work just to set up a method. Of course, if you have |
Howdy, I can't seem to work out a way to stub a protected method with an out param. Is it possible? Is/could there be something like
mock.Protected().Setup<Thing>("ImProtectedWithOut", ItExpr.IsAnyOut<int>())
?The text was updated successfully, but these errors were encountered: