-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
Provide a way to opt out of VerifyAll() #937
Comments
@ashmind, suppose there were a new
Would that be sufficient for you to be able to parameterize your helpers? You'd then use |
Would this require me to opt-in with My Project Scenario 1(most common, any developer in the project)
No Most of the time developers might not even be aware base class does that. My Project Scenario 2(rare, only a few most widely used shared helpers)
Can explicitly opt out of verification -- not perfect, but makes helper simpler and it's too widely used to guarantee both setups will be used (would be cool to guarantee at least one was used, but well). There are only a few, and the authors of those will be more experienced developers, aware of Verifiable(false) if added. |
Yes, and based on your two scenarios, I can see how this wouldn't help a lot. My immediate thoughts:
Any ideas? |
For the refactoring, it's a fair point, but the tests themselves are actually quite nice, even if infrastructure is a bit complex in places. Not unsimilar to Moq/xUnit, tbh, some complexity inside but most people just get the nice API. Completely agree that changing I'll try to think about some alternatives. One immediate option: what if the framework provided enough information for me to do the verification myself? E.g. add something like Pros:
Cons:
I'll think about some other ideas. |
To follow up on your idea above: I'd very much like to see the Let me check with Moq 5 whether it has anything like the |
@ashmind, I've done some quick prototyping. The following additions to Moq's API would allow you to discover all of a mock's setups and verify them. namespace Moq
{
partial class Mock
{
/// <summary>
/// Gets a list of live (i.e. non-overridden) setups on this mock.
/// Setups are returned in the opposite order in which they were added to the mock.
/// </summary>
public ISetupList Setups => ...;
}
/// <summary>
/// A list of setups on a mock.
/// </summary>
public interface ISetupList : IEnumerable<ISetup>
{
}
/// <summary>
/// Describes a setup on a mock.
/// </summary>
public interface ISetup
{
/// <summary>
/// Gets the expression that was passed to <c>.Setup()</c>.
/// Recursive setup expressions such as <c>mock => mock.A().B().C() are split up
/// and distributed over several mocks; see <see cref="ReturnsMockObject"/>.
/// </summary>
LambdaExpression Expression { get; }
/// <summary>
/// Gets a value whether this setup was marked as verifiable using the <c>.Verifiable()</c> verb.
/// </summary>
bool IsVerifiable { get; }
/// <summary>
/// Checks whether this setup is configured to return a mock object.
/// If so, the <see langword="out"/> parameter <paramref name="mock"/> will be set to the corresponding <see cref="Mock"/>.
/// </summary>
/// <param name="mock">
/// If this setup is configured to return a mock object, this parameter will be set to the corresponding <see cref="Mock"/>.
/// </param>
/// <returns>
/// <see langword="true"/> if this setup is configured to return a mock object;
/// otherwise, <see langword="false"/>.
/// </returns>
bool ReturnsMockObject(out Mock mock);
/// <summary>
/// Verifies this setup.
/// </summary>
/// <exception cref="MockException">This setup was not matched by any invocations.</exception>
void Verify();
}
} The Btw. such "inner mock" setups, when verified, verify all setups on the inner mock, which might not be super-obvious. What's still missing here is something for your tags. If I'm looking at the correct code, Moq 5 setups have a |
That looks great! Some notes:
You mean that calling
|
To be more precise here: There really is no
That's a reasonable request. Ideally, we'd distinguish between two expressions: the exact one that got passed to Note that if mock.Setup(m => m.GetA().GetB())...;
mock.Setup(m => m.GetA().Frobble())...; then
Yes, "inner mock" is what the child mocks are called internally, I've tried to keep it out of public parlance because I thought it would be too technical but I'll take your statement as proof to the contrary. :-) |
FYI @ashmind, I've pushed a release to NuGet, however that doesn't yet include the ability to tag setups. I haven't forgotten it, though. In the meantime, if you're in a hurry, you can verify single setups now, so it should be possible to loop over all setups and A future version of Moq will likely make that easier by adding arbitrary user state to setups, and through a new |
i came here looking for a Verifiable(false) to opt out of such a setup causing an exception wth VerifyNoOtherCalls(), is something like that supported? |
@ashmind, it's been a long time since anything happened here, and you've probably moved on. In case you're still interested, I'm probably going to merge #1319, which will give you a way to opt out of @drdamour, I'm not sure yet how this is going to affect |
My goal is similar to #747, so I am using
VerifyAll()
to detect unused setups. However there are a few very common setups done in common helpers that I don't want to detect.It would be great to have some way to opt-out of
VerifyAll
in specific cases, e.g. by usingVerifiable(false)
.The text was updated successfully, but these errors were encountered: