Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper source template adding empty statement

To ease testing, I put together a Resharper source template to to quickly create a substitute for a class with NSubstitute.

[SourceTemplate]
public static void substitute<T>(this T o,
    [Macro(Expression = "guessExpectedType()", Editable = -1)] string type)
    where T : class
{
    //$ o = Substitute.For<$type$>();
}

Assuming a field, private ILogger logger; has already been defined, this lets me type logger.sub (Resharper suggests the full method name from there) to generate logger = Substitute.For<ILogger>();.

I still have two issues I haven't been able to figure out.

1) Sometimes it puts an empty statement before the replaced code, i.e.

;
logger = Substitute.For<ILogger>();

This appears to happen whenever there are statements below the replaced line. Resharper will then suggest the empty statement can be removed. Why is this being adding to begin with, and how can I prevent it?

2) It only correctly guesses the substitute type if NSubstitute has already been imported for the class (using NSubstitute;). Without it, I get logger = Substitute.For<object>(); and a suggestion to import NSubstitute. Is there a better Macro expression to resolve the field type? Alternatively, is there a way to get it to import the namespace if it hasn't been imported already?

like image 942
J. McBride Avatar asked Mar 03 '26 03:03

J. McBride


1 Answers

  1. This sounds like a bug - please can you file an issue
  2. Try including the namespace in the commented code. ReSharper will shorten references and automatically add the namespace to the list of using statements at the top of the file:

    //$ o = NSubstitute.Substitute.For<$type$>();

like image 158
citizenmatt Avatar answered Mar 05 '26 16:03

citizenmatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!