devlooped / moq

The most popular and friendly mocking framework for .NET
Other
5.94k stars 802 forks source link

ToString() on SetupPhrase does not return expression #810

Closed jacob-ewald closed 5 years ago

jacob-ewald commented 5 years ago

We have a unit test that verifies the message we receive when a sequenced setup is not run, but with version 4.10+ the message is no longer displaying the expression. Instead, it displays the full name of the type and method, which is the default ToString() output. I originally thought this may be an issue with Moq.Sequences, however it seems to be originating inside Moq.

To reproduce the issue here is a simple unit test:

[Fact]
public void SetupPhraseConvertsExpressionToDescriptiveString()
{
    var setupPhrase = new Mock<IFoo>().Setup(x => x.DoThings(null));

    Assert.Equal("x => x.DoThings(null)", setupPhrase.ToString());
}

Expected:

x => x.DoThings(null)

Actual:

Moq.Language.Flow.VoidSetupPhrase`1[Moq.Tests.Regressions.IssueReportsFixture+IFoo]

I can submit a PR to fix this (simply overridding the ToString() on SetupPhrase), but wanted to see if that would be welcome first. Thanks!

stakx commented 5 years ago

@jacob-ewald: Sounds like a reasonable enhancement! I suppose it would make sense for all fluent API types' ToString methods to simply delegate to the ToString method of the setup that they're wrapping. Your PR would be welcome. It'd be great if you could make the change for all ...Phrase classes.

jacob-ewald commented 5 years ago

Thanks @stakx. I created PR #812. I did not add handling for WhenPhrase because that works on Func<bool>, which doesn't play nicely with the ExpressionStringBuilder.