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
Expose Fake.TryGetFakeManager and Fake.IsFake #1709
Comments
Hi, @asgerhallas. Thanks for suggesting this. My first reactions:
Counterpoints, @thomaslevesque? |
I don't see any harm either in exposing it either, but maybe not in its current form. Currently it returns the bool TryGetFakeManager(out FakeManager manager) Alternatively, we could expose a |
In fact, we could expose both |
Yeah, I was considering resignaturing the method as well. I… guess it's the right thing to do, even though the nullability of the return value (I'm skipping ahead a bit) would serve as well to indicate that there's no result. I think it comes down to whether we want to continue to support these established patterns or help establish new ones. Even though I think the usual signature is slightly antiquated, it's probably best to conform at this point? I've no objection to exposing |
Hi @blairconrad and @thomaslevesque, Per your points @blairconrad:
Just feels like a little too much indirection. I guess at some point I will get an error message, I won't understand, related to this :)
The utility stores fakes for later retrieval (in memory, in a list, very simple). And I want to allow users of the utility to configure the fakes e.g:
But also just fall back to defaults:
It seems minor, but when used in quite large test suite it means a lot.
I agree very much with this:
Just wish they'd made a `if (x.TryGet() is not null value)´ pattern matching thing, though I guess I could do somethng like this i C# 8:
So I'm all for "help establish new ones". Like FakeItEasy has done before :) |
Well, we're not going to establish new patterns on our own, I think... I used to dislike the Return null means the caller has to check for null. Using // existing TryGetValue method
if (dict.TryGetValue(key, out var value))
{
...
}
// vs hypothetical TryGetValue method returning null if not found
var value = dict.TryGetValue(key);
if (value != null)
{
...
} Of course, it's also possible to use
Yes, but it's inefficient... two steps instead of one. More code, and more work for the computer.
You can use |
Okay, @thomaslevesque. I'm convinced. On public static bool TryGetFakeManager(object potentialFake, out FakeManager fakeManager);
public static bool IsFake(object potentialFake); (If this gets added after nullability warnings are enabled, I'd mark the Should be fairly straightforward, even including updating the Advanced Usage docs. |
Yes, but...
That was the point I tried to get to with my switch and all. Sorry, I wasn't clear 🙂
Hmm... yeah, you've got a point. Composing is a tiny (really tiny) bit more work though:
Therefore I tend to choose the "new pattern" and rely on the Try-prefix to warn me that this could return null. I might change my opinion in the future though... I often do 🙂 But really not a real problem anyway. I'm just happy that you want to expose the functionality 🤗 |
Great, @asgerhallas.
The next question is whether you want to expose the functionality. The issue is near and dear to you. Are you interested in implementing it? |
Hi @blairconrad, sure! :) Is this usable #1711? |
Fix for issue #1709 - now targeting 5.x support
Closed by #1712. |
This change has been released as part of FakeItEasy 5.5.0. |
Thanks for proposing and implementing this issue, @asgerhallas. Look for your name in the release notes! 🥇 |
I wonder if it would be possible to make
Fake.TryGetFakeManager
public?I have a test utility that makes auto-wrapping fakes and I would like to ensure that the provided object is not already a fake - preferably without catching an exception :)
The text was updated successfully, but these errors were encountered: