Johan Larsson
Johan Larsson

Reputation: 17580

Is there a way to configure the variable name Resharper suggests?

I find myself writing new Mock<ISomeClass>(); then Alt + Enter The result is (R# creates a variable):

var mock = new Mock<ISomeClass>();

I want it to be:

var someClassMock = new Mock<ISomeClass>();

Is there a way to configure the name Resharper gives the variable?

Upvotes: 3

Views: 163

Answers (1)

Igal Tabachnik
Igal Tabachnik

Reputation: 31548

The closest I could recommend is creating a ReSharper live template to insert this for you. I use something very similar myself with FakeItEasy. Here's how you could do it:

  1. Create a new Live Template called (ReSharper -> Templates Explorer -> C# -> New Template)
  2. Set the Shortcut to mock, add a description
  3. Create this expression body: var $VARIABLE$Mock = new Mock<$TYPE$>();
  4. Important: make sure the order of the parameters is TYPE first, VARIABLE second
  5. Click Choose Macro on the VARIABLE parameter, and select "Value of another variable with the first character in lower case"

It should look something like this:

Now when you go to your code and type mock, you'll generate a pattern where you would type your class or interface name inside new Mock<>(), and it will prepend the name automatically. Sadly, I haven't found a good way to remove the i from the generated name if it's an interface, so that's something you'll have to do manually...

Hope that helps.

Upvotes: 1

Related Questions