-
-
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
Moq cannot mock a method that contains a default value of null w/out explicitly setting that value in the mock's expression tree #316
Comments
The error seems to be a compilation error, so, Moq can't change the
compiler, no
On Fri, Jan 6, 2017, 8:35 AM Michael McCarthy ***@***.***> wrote:
This is related to a MediatR 3.0 upgrade, but I'm sure the problem extends
to any method that is mock-able that uses a default value as one of its
parameters.
Here is the interface I'm trying to mock:
public interface IMediator
{
Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = null);
Task Send(IRequest request, CancellationToken cancellationToken = null);
Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = null) where TNotification : INotification;
}
We have thousands of unit tests that mocked the older version of this
interface (w/out the CancellationToken). An example of that code:
mockMediator.Verify(mock => mock.Send(It.Is<CampaignDetailQuery>(c => c.CampaignId == campaignId)));
after upgrading the library to the interface above, for each unit test I'm
getting this error:
An expression tree may not contain a call or invocation that uses optional arguments
We'd have to change thousands of unit tests to explicitly pass the default
parameter to the expression tree like this:
mockMediator.Verify(mock => mock.Send(It.Is<IndexQuery>(q => q.OrganizationId == null), default(CancellationToken)));
which is a lot of changes.
Any plans for Moq to support NOT having to explicitly pass in default
parameters in expression trees to mockable members?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#316>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAKW625YhHcHxIRtBG5_Pc2vAYuHvzSDks5rPkMvgaJpZM4Lct9q>
.
--
…--
/kzu from mobile
|
As long as Moq is based on expression trees, and that compiler restriction isn't relaxed, there's just no way to solve this. Here's a suggestion for a workaround that may or may not work in your situation.
(P.S.: Starting with C# 8 / .NET Core 3, you may be able to skip the base class and instead define the |
I got this working by doing the following:
and then
|
Thanks DaanDL, this worked for me. |
This is related to a MediatR 3.0 upgrade, but I'm sure the problem extends to any method that is mock-able that uses a default value as one of its parameters.
Here is the interface I'm trying to mock:
We have thousands of unit tests that mocked the older version of this interface (w/out the CancellationToken). An example of that code:
after upgrading the library to the interface above, for each unit test I'm getting this error:
We'd have to change thousands of unit tests to explicitly pass the default parameter to the expression tree like this:
which is a lot of changes.
Any plans for Moq to support NOT having to explicitly pass in default parameters in expression trees to mockable members?
The text was updated successfully, but these errors were encountered: