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?
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$>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With