JasonBock / Rocks

A mocking library based on the Compiler APIs (Roslyn + Mocks)
MIT License
243 stars 20 forks source link

Improve "No Handlers Were Found For" Message #338

Closed JasonBock closed 2 weeks ago

JasonBock commented 3 weeks ago

Describe the solution you'd like

Right now the message is formatted like this:

No handlers were found for Boolean IsEnabled(Microsoft.Extensions.Logging.LogLevel)

That's somewhat helpful, but what would be even better is:

No handlers were found for Boolean IsEnabled(Microsoft.Extensions.Logging.LogLevel)
   logLevel: 3

I'm not sure if can handle custom renderings for all types, but maybe I do something where I get the enum name and put strings in double-quotes. But the point is, log the values seen that don't have an expectation associated with it.

JasonBock commented 3 weeks ago

I'm thinking that I'll need to expose a formatter method in Rocks (or emit it like I do RefStructArgument). For a mocked method like this:

void DoStuff(string data, MyEnum switcher, int[] content);

I currently generate this:

throw new global::Rocks.Exceptions.ExpectationException($"No handlers were found for {this.GetType().GetMemberDescription(0)})");

I would generate code that looks something like this:

throw new global::Rocks.Exceptions.ExpectationException(
  $$"""
  No handlers were found for {this.GetType().GetMemberDescription(0)})
    data: {global::Rocks.ParameterFormatter.Format(@data)}
    switcher: {global::Rocks.ParameterFormatter.Format(@switcher)}
    content: {global::Rocks.ParameterFormatter.Format(@content)}
  """);

As a side note, I should start to consider moving everything in Rocks that is needed by the user to IncrementalGeneratorInitializationContextExtensions. That way I can change the NuGet package so that I don't have to copy the assembly into another location so types within it can be referenced. I'll put that in a different issue to track.

JasonBock commented 3 weeks ago

Also consider what needs to show with type arguments (if any).