Rickard Nilsson is a software architect, developer, craftsman, agile enthusiast, and father of three... More
Rickard blogs about crafting software using .NET tooling and solid, development practices.
I’ve been a heavy ReSharper user, fan, and addict since I first tried it back in 2007. Over the years I’ve developed the way I code leveraging the powerful productivity features of ReSharper and Visual Studio.
I present a series of my favorite Ninja tricks and workflows to inspire others to improve their skills and I encourage you to do the same, either in the comments section or elsewhere.
You can find this and other power tips with the ReSharper user tip tag
Category: Workflow
Following test-driven development I often define my unwritten classes by first using it in a unit test. I then use Create class and Create method or property to generate the code from usage. As the class gets created in the same file as the unit test I use Move to folder to put the class in the production code assembly. Namespaces and using statements are fixed automatically.
Place the marker on the class name in red and hit Alt+Enter
Place the marker on the method name in red and hit Alt+Enter
Place the marker on the class name and hit Ctrl+R, O. Then pick the folder you want the class to move to.
TDD Katas has become very popular in a small segment of the development community and we call our selves software craftsmen. We are passionate about software development as a craft and engage in different activities to better our selves and our peers.
My first kata cast, for instance, has been viewed close to 10k times on Vimeo since its publication. Much of the attention is of course due to Roy Osherove linking to my blog post from his TDD Kata 1 page. This time Roy initiated a sequel, meant to introduce interaction based testing using mocks and possibly stubs, and continue the teaching process of TDD and unit testing practices.
The following screen cast covers the entire kata in .NET, complete with Osherove’s three steps as well as manual UI testing at the end.
For best viewing experience I recommend watching it on Vimeo.com in HD
String Calculator TDD Kata 2 - Interactions from Rickard Nilsson on Vimeo.
The tools I use are Visual Studio, ReSharper, TestDriven.NET, Moq for mocking, and NUnit.
The code and Visual Studio solution for the finished Kata can be downloaded from GitHub:
Download source
As Osherove mentions in his instructions, this kata is not as simple as the first part, nor as simple as most katas out there. The reason is the element of interaction based unit testing involved, which is quite difficult to wrap you mind around, and it took quite a while to get the steps right. I thought I should share my path to the kata in its present form for others to learn from and comment on.
Step 1. Everytime you call Add(string) it also outputs the number result of the calculation in a new line to the terminal or console. (remember to try and do this test first!)
As I did this test first I started out pretty much as how it ended up in the cast. However, after a while I tried to take a step back and see if there were any smells in the code I had not yet discovered.
I found that I didn’t really like the mixed responsibilities that the Calculator class got when I introduced writing to the console. This could be seen as a logging feature and thus a perfect candidate to become an aspect (in AOP). I started playing around with PostSharp and ended up with the following solution which is quite clean.
[Serializable] public class OutputAttribute : OnMethodBoundaryAspect { [NonSerialized] private IContainer container;
public override void OnExit(MethodExecutionArgs args) { var console = container.Resolve<IConsole>(); console.WriteLine(args.ReturnValue.ToString()); }
[OnDeserialized] public void OnDeserialized(StreamingContext context) { container = ContainerFactory.Current; } }
Which, at most, leaves the mark of a custom attribute in the Calculator class:
public class Calculator { [Output] public int Add(string value) { ... } }
The problem with this solution is the way PostSharp works. It does all its magic as a post compilation step so everything is pretty much static. This is a problem in a testing scenario when we need to inject the mocked console in this case, hence the smelly ContainerFactory.Current stuff.
Another problem with this solution is that in part three, the console app, we need to disable or override what is outputed. This ends up becoming a static mess which did not feel right at all. If you have another view on this please leave a comment.
Step 2. Create a program (test first)that uses string calculator, which the user can invoke through the terminal/console by calling “scalc ‘1,2,3’” and will output the following line before exiting: “The result is 6”
For step 2 and 3 I thought a bit about refactoring to a UI design pattern like MVP, MVC or MVVM but finally decided to drop it, mainly because I didn’t know any framework like that for console applications. If the application grows I think this is the right way to go, but for the known requirements it’s an overkill, especially considering how small the solution is.
Step 3. Instead of exiting after the first result, the program will ask the user for“another input please” and print the result of the new user input out as well, until the user gives no input and just presses enter. in that case it will exit.
I played around a bit with SpecFlow, which has ha free form Given/When/Then specification syntax, on the later part of the kata. However, I felt that I lost velocity so I dropped it as well. Maybe, if I had some way of conducting complete acceptance testing through a real console, I would have pursued this further. It was simply too much to write, for example:
Scenario: Prompt user for another input Given a new string calculator And the user has entered: a valid input When the program has outputed The result is 1 Then the user is prompted for another input Scenario: Quit on empty input Given a new string calculator And the user is prompted for another input When the user hits enter Then the program should exit
Scenario: Prompt user for another input
Given a new string calculator
And the user has entered: a valid input
When the program has outputed The result is 1
Then the user is prompted for another input
Scenario: Quit on empty input
And the user is prompted for another input
When the user hits enter
Then the program should exit
Figure 3. SpecFlow Feature specification for the console app If you have any thoughts, comments, suggestions, or any other feedback please leave them below or ping me on twitter.
Are you new to the concept of code katas? Read my previous blog post and watch me perform the String Calculator Kata.
In my never ending goal of self improvement in the techniques and tools I use I’ve been practicing a version of the Prime Factors Kata for a while.
The Prime Factors Kata, initially sparked by the infamous Uncle Bob Martin, is about finding an arbitrary number’s prime factors. In the cast I show how my TDD practice has evolved into a flavor of BDD, mainly to reduce duplication in the unit tests. I also show off the awesome power of my current toolset which includes the Visual Studio 2010 and the latest versions of ReSharper, TestDriven.NET, NUnit and NBehave.
Though my performance is not yet perfected I want to put it out there because I feel there are no C# version that can really match the Ruby version in elegance and wit. This is my attempt to show what you can do with the C# language when you know the frameworks really well.
Please leave comments and/or suggestions below or record your own kata session in response.
Prime Factors Kata in C# from Rickard Nilsson on Vimeo.
If you are new to the Prime Factors Kata, code katas in general, or TDD for that matter, you may find the steps I take unnecessary or weird. You may want to watch the annotated version in which Uncle Bob explains why each step is taken and why they are taken in that order.
Many have recorded there own versions of the Prime Factors Kata which all inspired me in the way I practice it. The cast that inspired me the most is
there are also a few other C# casts worth watching for comparison by:
Moles is a new framework from Microsoft Research for isolating objects in unit tests. With the framework you create test stubs by using delegates and you can route any .NET method you want, including non-virtual and static methods in sealed classes. In addition, the framework is free, making it a major competitor to TypeMock Isolator that has been alone on this functionality for a long time.
Moles automatically generates stubs for all classes in one assembly. Here is an example of how easy it is to change the behavior of the static DateTime.Now property:
// change the value of DateTime.Now MDateTime.NowGet = () => new DateTime(2000,1,1); if (DateTime.Now == new DateTime(2000,1,1)) throw new Y2KBugException();
Mole’s strength to fake and reroute static methods and the like makes it a very powerful tool for isolating and unit testing code developed for SharePoint. Microsoft Research has a whitepaper that describes how to get started:
Unit Testing SharePoint with Microsoft Foundation Pex and Moles
Here is a video that introduces Moles:
After I posted my Code Kata Cast I received some feedback regarding the ReSharper templates I use to speed up my coding. I decided to share them with the public (like so many before me) in hope that others may benefit from them, as I do.
rickardn-resharper-templates.zip (1,44 kb)
After you’ve downloaded the zip-file and unpacked it, open Visual Studio and the ReSharper Templates Explorer: Menu –> ReSharper –> Live Templates…
Click on Import… as the screen shot below shows, and find the file “rickardn-resharper-live-templates.xml”
Then click the “File Templates” tab and repeat the procedure for the “rickardn-resharper-file-templates.xml” file.
Good luck with your katas!
Have you ever come across the concept of a Code Kata?
For me it really took off after reading blog posts (1, 2, 3) by Unce Bob Martin and Pragmatic Programmer Dave Thomas. The concept is really simple: how can we, as programmers, better our selves and improve our techniques and proficiency in using the tools and processes in our every day work?
The suggested solution is inspired by the martial arts kata. You learn how to implement a solution to a specific problem and you practice all the moves in the exact same order over and over again. The point is that you should know the moves so well that you forget about them and focus on improving your key strokes and the use of your tool set. The never ending goal is to perform the kata with the least amount of key strokes.
The promise is that practicing these kata's often and regularly makes you a better and more productive programmer in that you are trained to act instinctively in certain reoccurring situations.
Anyway, I've been practicing a kata based on a problem initiated by Roy Osherove and I decied to record it to get some feedback and maybe spread some knowledge on how I practice Test-driven development using ReSharper.
Calculator Code Kata Cast 1 from Rickard Nilsson on Vimeo.
I've been working to get my head around the Model-View-Presenter pattern and I've focused primarily on a Winforms scenario. As it turns out there are numerous sources out there on MVP, some of which are focused on Winforms, e.g. [2, 8] and some which are not focused on Winforms but highly valuable just the same, e.g. [1, 4].
Figure A. Screen mock up
However, none of the referenced sources discusses the issue of communication between MVP triads. All of them thoroughly discusses the ins and outs when you have a single presenter talking to a single view and a single model, but nothing about communicating with other MVP sets. I've also found that other people are also struggling with this problem and how to best solve it but no really good solution has presented itself.
Well, we start with an example to formulate the problem. If we look at the example of the humble dialog [8] this only covers a single triad so we make it slightly more complicated. We have two views, a parent view and a dialog view, and each view has an associated presenter, i.e. parent presenter and dialog presenter. Now, the requirement is that when a user clicks a button on the main view the dialog should pop up modally giving the user the possibility to change a property value. The value change should then be reflected in the parent view. Sounds simple enough, right?
The problem is to implement this leveraging the MVP pattern to achieve a loosely coupled, highly cohesive, and test friendly solution. Some of the questions are; who has the responsibility to create the dialog view, who has the responsibility to show the view, what is the result from the dialog view and who should act on it.
There are many different ways we can go about doing this but since I'm into Test-Driven Development I'm gonna do it test first. I also like to do UI up front so I create a Winforms project in Visual Studio and start dragging and dropping. The simplest possible screens are shown in figure A.
OK, now let's start with our first test. We want the dialog to appear when the "Edit user name" button is clicked and following MVP the view should simply pass on control to its presenter. The parent presenter should then have the dialog view popping up. This is the first test using Rhino Mocks [3]:
[Test] public void ParentPresenter_EditUsername_should_open_a_dialog() { var view = mockery.DynamicMock<IParentView>(); var dialogPresenter = mockery.CreateMock<IDialogPresenter>(); using (mockery.Record()) { dialogPresenter.Load(); }
using (mockery.Playback()) { var presenter = new ParentPresenter(view) { DialogPresenter = dialogPresenter }; presenter.EditValue(); } }
The parent presenter should simply pass the control to the presenter of the dialog. Thus, the parent presenter has a dependency on the presenter of the dialog and the dependency is injected through a property:
var presenter = new ParentPresenter(view) { DialogPresenter = dialogPresenter };
Now, these interfaces and classes doesn't exists so first we have to create them. But before we do I see some code that has no test, namely dialogPresenter.Load(), so let's write another test:
[Test] public void DialogPresenter_Load_should_call_show_on_view() { var view = mockery.DynamicMock<IDialogView>();
using (mockery.Record()) { view.Show(); }
using (mockery.Playback()) { var presenter = new DialogPresenter(view); presenter.Load(); } }
So, the presenter simply tells the view to show itself. Remember that we're talking to an interface and that the view in the test is not the real implementation. We're only driving out the behavior of the presenter at this point but we're doing it in such a way that the view implementation should be as thin a possible.
I'm using ReSharper [7] to generate the interfaces and classes that I dreamed up in the tests.
When the initial tests are passing let's move on. The next thing that should happen is the user providing a new user name in the dialog and clicks on OK. So lets write a test for the OK event. I realize that some refactoring is also in place:
[Test] public void DialogPresenter_ChangeName_should_change_value_and_close_dialog() { var model = mockery.CreateMock<User>(); var callBack = mockery.CreateMock<Action>(); var expectedName = "foo";
using (mockery.Record()) { Expect.Call(dialogView.Username).Return(expectedName); dialogView.Close(); callBack(); }
using (mockery.Playback()) { IDialogPresenter presenter = new DialogPresenter(dialogView) { Model = model }; presenter.NameChanged += callBack; presenter.EditName();
Assert.That(model.Name, Is.EqualTo(expectedName)); } }
The last requirement states that the user name change should be reflected in the parent view. There are a couple of ways to do this but in this example I'm gonna go with the Observer synchronization [6] strategy because it provides a nice separation of concerns and is easy enough to mock. Note, however, that it is the presenter which acts as the Observable, not the domain object itself (User in this case). This is because I do not want to polute the domain model with event code. The previous test verified that a callback method is called whenever the name in the dialog is changed. Now, the parent presenter only has to hook up an event handler to the dialog presenter's event. Our next test shoes what should happen in the parent presenter when the event is triggered:
[Test] public void ParentPresenter_Update_should_update_view() { var model = new User {Name = "foo"};
using (mockery.Record()) { Expect.Call(parentView.Username = "foo"); }
using (mockery.Playback()) { var presenter = new ParentPresenter(parentView) { Model = model }; presenter.Update(); } }
So, the presenter explicitly tells the view what to do. The parent view is an example of a Passive view [5]. The presenter's code is included below for completeness:
// Parent Presenter public class ParentPresenter { private readonly IParentView view;
public ParentPresenter(IParentView view) { this.view = view; this.view.Presenter = this; Model = new User(); DialogPresenter = new DialogPresenter(new DialogView()) { Model = Model }; DialogPresenter.NameChanged += Update; }
public IDialogPresenter DialogPresenter { get; set; } public User Model { get; set; }
public void Load() { view.Show(); }
public void EditValue() { DialogPresenter.Load(); }
public void Update() { view.Username = Model.Name; } }
// Dialog Presenter public class DialogPresenter : IDialogPresenter { public event Action NameChanged; private readonly IDialogView view;
public DialogPresenter(IDialogView view) { this.view = view; this.view.Presenter = this; }
public User Model { get; set; }
public virtual void Load() { view.Show(); }
public void EditName() { Model.Name = view.Username; view.Close();
if (NameChanged != null) NameChanged(); } }
And then the view implementation which is as minimalistic as possible:
// Parent View public partial class ParentView : Form , IParentView { public ParentView() { InitializeComponent(); }
public ParentPresenter Presenter { private get; set; }
public string Username { set { username.Text = value; } }
void IParentView.Show() { Application.Run(this); }
private void editValueButton_Click(object sender, EventArgs e) { Presenter.EditValue(); } }
// Dialog View public partial class DialogView : Form , IDialogView { public DialogView() { InitializeComponent(); }
public IDialogPresenter Presenter { private get; set; }
public string Username { get { return nameTextBox.Text; } }
void IDialogView.Show() { ShowDialog(); }
void IDialogView.Close() { Close(); }
private void acceptButton_Click(object sender, EventArgs e) { Presenter.EditName(); } }
The way the MVP parts are separated makes a UI with allmost 100% test coverage but above all the UI logic is in a separate class which is not tied to a frame, thus enabling us to discover duplicated code and oportunities to refactor and keep the UI maintainable. I think that this is a great way of driving out a design since I start out with the client hat on and not the other way around were I first list a bunch of methods and properties on a class. IMO this kind of interaction based testing really lends itself to driving out interaction design, and tools like ReSharper really makes the developer experience pleasant and productive.
[1] Jean-Paul Boodhoo. Design Patterns: Model View Presenter [2] Dan Bunea. Model View Presenter - is testing the presenter enough [3] Oren Eini. Rhino Mocks - dynamic mocking framework [4] Michael Feathers. The Humble Dialog Box [5] Martin Fowler. Passive View [6] Martin Fowler. Observer Synchronization [7] JetBrains. ReSharper [8] Jeremy Miller. A Simple Example of the "Humble Dialog Box"
Proceeding with a solution there is no way of changing the implementation of the GregorianCalendar class to get the correct behavior so we have to go about this another way. One is to subclass the GregorianCalendar and override the GetWeekOfYear method. Another is to implement the GetWeekOfYear in a class totally separate from the .NET framework. I think that the first one is the way to go since the error that we're trying to correct is in the framework itself.
So, first we alter the help method in the test case to use our sub classed IsoCalendar (which we haven't written yet).
So, we are not in a compile state yet so lets do the simplest thing to get this running. I decided to subclass the GregorianCalendar class since what is wrong is the GetWeekOfYTear method of that class. I basically override that method like below.
We run the test a get a red bar. Fine, but now lets go ahead and implement the algorithm. However, this has been done over and over with varying elegantness. My favorite is the work of programmer and mathematician Julian Bucknall. His original implementation can be found at his blog. So, instead of returning -1 I call the GetIsoWeek method from Bucknall's implementation and take the modulus of 100 (since the result from the algorithm is of the form YYYYWW). We run the test and we get a green bar!
Now when we get a green bar there's just one more thing to add. The ISO 8601 week should only be returned when the parameters rule and firstDayOfWeek is set properly according to the documentation. In all other cases the GregorianCalendar implementation works just fine. We fix this with a guard clause at the top and we're done.
During this series we covered test coverage for the faulty implementation of weeks according to ISO 8601. We used Test-Driven Development to provide a fix using Julian Bucknall's really smart implementation. Next we should take a look at refactoring to design patterns.
According to the ISO standard, in the period 4 January - 28 December the week number is always the same as the Gregorian week and the same also apply for all Thursdays. Thus, the mentioned dates can be safely excluded in our test coverage. Though we still have an infinite number of dates to cover, we are getting there. Finally, we can limit our test coverage to a set of years that can be reasonable argued to appear in the application which we are implementing.
To simplify it the resulting dates are non Thursdays 1-3 January and 29-31 December every year from 2005 - 2015.
Using the calendar in Outlook I compile a matrix of the week numbers in the year interval. The dates that are grayed are Thursdays which should not be part of the test.
The matrix can be thought of as a set of { date, week number } pairs which can easily be implemented in code.
I create a test which iterates over the set and makes assertions that the correct week was generated by the calendar. It turns out however, that only eleven of the dates generate a wrong week number. I refactor the code to only include the weeks that are wrong. I get a red bar and I see another opportunity for refactoring, the previous two tests can be integrated with this test by just adding the date - week pair to the set. The following is the resulting test:
Now that we have a test that fails we can come up with a solution to get the test to pass.
Abstract So, now we make up another test. Looking at the calendar we see that new years eave 2001 should also belong to the first week of 2002 according to ISO 8601. We write the test much like the previous test and we get another red bar. Now its time to refactor. We need to remove the duplicated code in the two tests. First we start with extract method which results in the following method:
Now, we can refactor the tests using the new method like this: