Sunday, July 12, 2009

VS 2010 Generate From Usage

Microsoft has been adding more and more features to Visual Studio to encourage Test Driven Development. One of the new features in VS2010 for TDD is “Generate From Usage.”
One of the tenants of true test driven development is that you should write your unit tests before you write your code. One little tricky part of this in Visual Studio is that you loose the help of IntelliSense and sometimes it will even get in your way. To get around this problem, developers will often stub out their classes and methods first, and then write their tests. Generate From Usage in VS2010 provides another way to do this.
To try this out we will create a very simple math class called MyMath that has one method call Add(n1,n2) that will add two numbers together and return the result. First create a solution with a new C# Test Project and C# Class Library Project. In the TestMethod1 method of the UnitTest1 class type the following code (type it instead of cutting and pasting to see the full effect of this feature):
MyMath math = new MyMath();
Assert.AreEqual(3,math.Add(1, 2));
First thing you will notice is that when you type “new” and press the space bar, IntelliSense will popup and you will see the MyMath class on the list even though we haven’t defined it yet. The editor figures out that you are intending to create a class called MyMath so it puts in on the list.
As expected you will get red squiggles under MyMath since it doesn’t exist yet. At this point you would normally go and manually stub out this class, but Generate From Usage makes this easier. If you right click on MyMath and select Generate/Class, Visual Studio will automatically create a new stub for the MyMath class. By default it will put the new class in the test project which is probably not where you want it. To get around this instead of clicking Generate/Class, use Generate/Other…, this will pop-up this dialog box:

This give you more control over the generation of the code. In the location section you can change the project so that the new file goes to the Class Library project. Now if you build the solution the squiggles under MyMath will go away and the Add function will be flagged instead.
Now you can right-click on Add and select Generate/Method Sub and this will generate a method stub in the MyMath class. In this case there is no Other option since there really aren’t any other options for creating the Method since to be usable it must be in the MyMath class, must be public and must be called Add.
There is one tricky thing with this feature that I will demonstrate with another example. Let’s say we want to create a new class called Host. In TestMethod1 type Host and press the space bar. When you do this IntelliSense will try to be helpful. Since there isn’t a class called Host in the default namespaces it will put in something that is there; HostTypeAttribute. This is not what we wanted. To get around this there is a new feature in IntelliSense called Consume-First Mode. When the IntelliSense box pops up press Ctrl+Alt+Space to toggle to consume-first mode. In this mode instead of forcing you into a class that already exists it will just accept what you typed when you press the space bar. Once this mode is turned on it will stay in this mode until you press Ctrl+Alt+Space again while an IntelliSense window is open.