Archive for the ‘Visual Studio’ tag
Visual Studio/Resharper: Changing the order of arguments
We’ve recently run into some places in our tests where the expectation and actual values passed into NUnit‘s ‘Assert.AreEqual’ are the wrong way round, therefore meaning that the error messages we get when tests fail are somewhat confusing!
Assert.AreEqual(theActualValue, "the expectation");
We can change the arguments around using Resharper by using the key combination ‘Ctrl-Alt-Shift-ArrowKey’ but you can only do this one line at a time which was a bit annoying as there were about 20 to change.
I got a bit bored of doing this after a while so I thought I’d look into whether it would be possible to do this with a ‘Find & Replace’.
After a bit of trial and error this is what I’ve ended up with:
- Select all the areas of code that you want to change and press ‘Ctrl-H’
-
In the find box type:
\({.*}, {\".*\"} -
And in the replace box type:
(\2, \1
The ‘{}’ define a matching group of which we define two in this case and then switch them around. Visual Studio’s regex seems a bit different than the one I’m used to – the reference list for the syntax is available on MSDN.
It’s not too complicated and I’m sure there are edge cases where it wouldn’t work but for the little case I had it did the job reasonably well.
Resharper templates
One of the first things that I do when I go onto a project is setup a ReSharper template for writing tests.
I generally set this up so that when I type ‘should’ I can press tab and it will automatically create an outline of a test method for me.
Creating a template is as simple as going to ‘ReSharper > Live Templates’ from Visual Studio.
I have attached several templates that I seem to end up writing over and over again.
To import these go to ‘ReSharper > Live Templates’ and click on the Import button on the menu.
To make class creation templates available on the ‘Add New Item From Template’ menu right click on the project, ‘Add > New From Template > More’ then select the appropriate template and tick the box which says ‘Add to quicklist’
IntelliJ style item tracking in Visual Studio
One of my favourite features of IntelliJ is that it tracks the item that you currently have open on your Solution Explorer.
I thought this wasn’t possible in Visual Studio and had resigned myself to trying to remember which project each file was in. Luckily for me a colleague pointed out that it is in fact possible but is just turned off by default.
Tools > Options > Projects and Solutions > Check ‘Track Active Item in Solution Explorer’

Job done!