Skip to content
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

Cannot set callback on function call that uses default parameter #689

Closed
Custodia opened this issue Sep 7, 2018 · 4 comments
Closed

Cannot set callback on function call that uses default parameter #689

Custodia opened this issue Sep 7, 2018 · 4 comments

Comments

@Custodia
Copy link

Custodia commented Sep 7, 2018

If you setup a callback on a function with a default parameter and then call the function without overriding the default parameter the callback function supplied to Moqs .Callback will never be called.

@stakx
Copy link
Contributor

stakx commented Sep 7, 2018

@Custodia: The following test does what you've described, and it passes just fine:

public interface IFoo
{
    void Do(int arg = 42);
}

[Fact]
void Callback_invoked_if_mocked_method_called_without_explicit_value_for_default_parameter()
{
    bool callbackInvoked = false;

    var mock = new Mock<IFoo>();
    mock.Setup(m => m.Do(It.IsAny<int>())).Callback(() => callbackInvoked = true);

    mock.Object.Do();

    Assert.True(callbackInvoked);
}

Can you post a brief failing test that demonstrates your problem?

@Custodia
Copy link
Author

Custodia commented Sep 9, 2018

I'll look at this again on Monday.

@Custodia
Copy link
Author

Custodia commented Sep 10, 2018

I'm actually not sure what I changed to resolve the issue but it does not indeed seem to be related to what the default parameter after all.

@stakx
Copy link
Contributor

stakx commented Sep 10, 2018

Well, I'm glad to hear you got things working. 👍

@stakx stakx removed the needs-repro label Sep 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants