Thursday, November 21, 2013

TDD Controller creation from unit test - Part 1

The dev team that I am currently on is making a huge effort to adopt TDD.  One discussion that comes up every now and then is what to do with your MVC controllers when it comes to TDD.  A couple of us say that you should still start with your test and a few say that it is just too easy to create a control without a test and starting from your test is just a waste of time.  This post is going to show you how easy it is to start with your test first and follow TDD the correct way using a few Resharper shortcuts.  These shortcuts are not required and there are other ways to accomplish the same thing, but Resharper does make it easier and quicker.

Do not be intimidated by the number of steps below.  I recorded the steps and it took less than two minutes to perform all steps.  You can see the recording of the steps in this video below.





1. The first thing that you need to do is create a blank solution and then add an ASP.NET MVC 4 Web Application and Unit Test Project.  If you don't know how to do this, you can read my my post about and it here.  When you are done, your solution should look similar to the following:





















2.  Add a Unit Test to your test project by right-clicking on your test project, then selecting Add -> Unit Test...










3.  Delete the provided TestMethod and use the Test Method Snippet that I introduced you to in my first blog post to give you the following test method stub.











4.  After the Assert comment, type the following:

     Assert.IsNotNull(homeController);

5.  With your cursor at the end of the line you just typed, type Alt-Enter and you should see something similar to the following:










6.  Select "Create local variable 'homeController'" by typing Enter






7.  Type the End Key















8.  Type Ctrl-X and move your cursor to just below the Arrange comment and type Ctrl-V to get your method to look like the following:












9.  Change object to var, type End, type Backspace, then type  = new HomeController();  You should now have the following:











10. With your cursor still positioned at the end of the line, type Alt-Enter and then select "Create class 'HomeController'"









11.  A class named HomeController will be created below your test method as seen  below.














12. Type End and then F6 to get the following menu:








13.  Select 'Move to Folder'.  This will open the Resharper Move to Folder dialogue as seen below.




















14.  Change the Target Folder to the Controllers folder in the ASP.NET MVC 4 Web Application that you created in step #1.  In my case, it is Namyaf.WEB\Controllers as seen below.









15.  Click Next and you will see your new file within the location you specified.













16.  Open HomeController.cs

17.  Make the HomeController class inherit from Controller

18.  Add the following using directive

     using System.Web.Mvc;











19.  Build the solution.  If you have errors, correct them.  If you cannot correct them, let me know and I can help.

20.  If you run your test within MSTEST or Resharper, you will see that the test passes.


Resharper test
MSTEST


















I hope you enjoyed this post and learned something.  In part two, I will be adding the controller actions and continuing with a little TDD.

peace yo!


Wednesday, November 13, 2013

F#

I decided recently that I was going to become a better programmer.  Not that I am a bad programmer, but I know that I could be better.  A few things that I think will make me, as well as others, become a better programmer are as follows:

1.  Start this blog
2.  Read more blogs
3.  Watch more technical videos
4.  Attend more live technical events
5.  Write more clean code
6.  Learn more programming languages or programming related technologies.

If you have read any of my posts, you know that I have been doing most of these already.  One item that has been on my list for a while falls into #6 on the list and that is the attempt to learn F#.  This post is not going to teach you F#, but may influence you to at least execute some F# code.  What I wanted to do for this post was to share my first real step in learning F#.

After searching for for a quick tutorial on F#, I found one and decided to open Visual Studio and begin the journey into F# land.  Before I could create a blank solution to determine what project type to add to my blank solution, I noticed a type of project named F# Tutorial.  Not knowing what this really was, I decided to select this one and take a look.












After creating the project, I remembered doing this in the past with Visual Studio 2010, but right away I noticed that the samples provided were a little different.  What I mean by that is that within the first few lines, which are comments, there is mention of something called F# Interactive and how you can highlight a section of the code and execute the code within F# Interactive.  If you are using Visual Studio 2010, the F# Tutorial project does not mention F# Interactive even though it is available to use.  Someone was smart and added the comments below to the Visual Studio 2012 version of the Tutorial.






So, I gave it a try and it was that easy.  The samples were enough to get me started and had simple string examples, lists, classes, recursive functions, and also an example connecting to a database.  Below are the two steps that I took to execute my first F# code within F# Interactive:

1. Highlight the Strings example

















2. Either Press Alt-Enter or right-click and select Execute in Interactive  



















As you can see it is pretty easy to execute F# code in F# Interactive and be on your way to being a better programmer.  I'm not sure when or if I will ever use it, but at least I know a little more about F# than I did yesterday.

peace yo!

Monday, November 11, 2013

dotPeek

I am in the beginning stages of a project where I need to create a class library that sort of acts as a proxy that will call a third party assembly.  The third party assembly is being called directly and I need to sort of duplicate some of the code.  Without going into too much detail, I will explain the issue and how I solved it.  Basically, this third party assembly was being called something similar to the following:

Dim resp As String = SubmitTransaction(req, ThirdPartyUtil.RequestId)

My first issue is that the code is in VB.Net, but that discussion is for a future post.  My next issue was that I needed to know what ThirdPartyUtil.RequestId is and how do I duplicate that in my code.  After doing some more research, I found another project that used the same third party assembly, but a little differently as you can see below:

string resp = SubmitTransaction(req, MainAssembly.GenerateRequestId());

As you can see, this code is much better because it is in C#, but it still has something that I don't know too much about and that is the call to MainAssembly.GenerateRequestId().  What I had to do was look through the VB.Net and C# code to try and figure out what this property and method call did.  Well, after getting lost and going nowhere, I asked a co-worker about it and they sent me some documentation on the assembly.  This didn't help much, so what could I do next?  After thinking about it for a few, I decided to see if dotPeek would provide any details about it.  If you are not familiar with dotPeek, it is a tool by JetBrains that can be used to decompile assemblies and I highly recommend at least installing it and learning what it can do.  This tool can be downloaded for free at http://www.jetbrains.com/decompiler/.  Now, without showing details on that exact assembly loaded in the tool, I was able to pull out the method and property and they ended up looking like the following:

MainAssembly:

public string GenerateRequestId()
{
   return ThirdPartyUtil.RequestId;
}







ThirdPartyUtil:

public static string RequestId
{
   get
   {
      return Guid.NewGuid().ToString("N");
   }
}





This was great news to me and will allow me to move forward without wasting too much time.  All I needed to do was to provide this same functionality which is to return the guid in the same way the code above did.  

For a quick demo on dotPeek, visit the main page of dotPeek and view the video which is only about five minutes long.  Also, if you don't use resharper download the trial version from JetBrains while you are there.  

peace yo!

Sunday, November 10, 2013

Future of Coding?

Last month, I attended a meetup with the group named Software Craftsmanship McHenry County where "Uncle Bob" Martin gave a presentation on Clojure which is a functional programming language.  I don't know much about functional programming languages, but I was able to keep up with Uncle Bob for most of the presentation. Although most of the discussion was on Clojure, Lisp, functional programming, and monads, the beginning was the most important and interesting part for me.  The whole presentation is pretty good but I would recommend watching the video starting from 6:50 through 24:30 if you aren't really interested in functional programming details and functional coding yet.  This  portion of the video discusses the future of threads, cores, solid state drives, databases, data access, no sql, CRUD database, and how writing code may be different in 5 - 10 years.  This portion is basically the lead in to how functional programming might be the solution.

The whole video can be found on YouTube here.

peace yo!

Friday, November 8, 2013

Blank Solution (Visual Studio)

A couple weeks ago, I was reviewing a project that a friend of mine was working on and noticed that he had a solution named similar to ProductName.Web.  This solution had an Asp.Net MVC Application with the same name along with other non web type of projects.  I asked why he didn't have a main solution named ProductName with other projects under that named ProductName.Web and ProductName.Whatever.  He said he didn't know of a way to do that, so I mentioned starting off with a blank solution.  To my surprise he was unaware of how to create a blank solution.  Fast forward a week or so later where I mentioned this to a co-worker and they informed me that they didn't know either.  Knowing that some people do not know that the blank solution exists confirms my belief that nothing is too simple to write about.  There is always something that you know that others don't and always something that others know that you don't know.

To start with, I wanted to list a few benefits of creating a blank solution first and they are as follows:

1.  Better grouping.
2.  Avoid having to rename things (projects, folders, etc.) in the future.
3.  It is clean
4.  It is what the cool kids are doing these days.

Steps for creating a Blank Solution and adding projects to it

1. Open Visual Studio and select File -> New -> Project







2. Expand Templates -> Other Project Types -> Visual Studio Solutions and then add a name to the Name field. In this case, I named it Namyaf and then clicked OK.


3.  As you can see, the blank solution is created with nothing in it.  


4.  If you open Windows Explorer and go to the location of your new solution, you will see one solution file.


5.  One thing that some people don't know that I wanted to mention here is that solution files are just text files and you can view them in notepad as seen below.  This one is very basic as you can imagine, but they will get filled with other stuff as projects are added.














6.  The next step is to add a project to the solution that you just created.  To do this, right click on the solution in Visual Studio and select Add New Project.





7.  Select Asp.Net MVC 4 Web Application, type Namyaf.WEB, and then click OK..


8.  Select the Empty template and also make sure the Create a unit test project is checked because we all know TDD is the way to go.


9.  Now, when you browse to your solution folder in Windows Explorer, it is much cleaner as you can see below.


Once again, I hope you learned something and enjoyed reading and following along.

Peace Yo!