CodeMash – Practical B/TDD

Well lunch was good, still at CodeMash.  Now sitting down in the afternoon session on Practical Behavior/Test Driven Development.  Apparently supposed to have some prereq software, so I’m trying to set those up on my machine rigth now.  Presentation is being given by a gentleman by the name of Philip Japikse.

Reviewed the concepts of User Stories and Context Specification.  Apparently these underpin the usage of MSpec, a tool for automating the running of user tests.  MSpec allows you to write Context Specification tests that group unit tests into a context of a feature expressed as a context specification.  The point apparently, is that MSpec will autogenerate HTML reports that can be shown to business analysts and other business stakeholders so they can understand what is being tested and how it’s being tested.

An example of a template of a test using this syntax looks a little like the below:

using Machine.Specifications;
using MbUnit.Framework;
using Rhino.Mocks;

namespace ThirdPartyToolSetupConcerns
{
    [Subject("0-Complete Application Concerns"), Tags("0-Complete Application Concerns")]
    public class When_Setting_Up_Third_Party_References
    {

         Establish context = () =>
	{
	   _toolMock = MockRepository.GenerateMock<ItoolRepository>();
	};

         Because ACTION;

        It Should_Get_List_Of_Tools_From_Command_Line = () =>
	{
	        _toolMock.Expect(tm => tm.HasTool()).IgnoreArguments().Return(true);
		var toolValidtor = new ToolValidator(_toolMock);
		Assert.IsTrue(toolValidator.HasTool());

	};
        It Should_Look_Up_Files_Required_For_Each_Tool_From_Database;
        It Should_Copy_Missing_Or_Updated_Files_Into_Correct_Directories_For_Each_Tool;
    }
}

The “Establish” and “Because” are delegates created to allow the test to be written in a context specification manner.  It seems to me that each “IT” is a separate actual unit test.  When run looks a lot like running tests in nUnit as long as you’ve named your test classes and test methods in a meaningful manner.  I think the value of MSpec is that it generates reports that are nicely formatted for your business stakeholders and such.

To implement the tests you use lambda expressions with no parameters, hence the ” = () => ” syntax, to create the code that does the setup and the code that implements each test in each one of the “IT” areas.  If no closure after an “IT”, then that test hasn’t  been implemented yet.  Above there is one closure for setup code and one test is implemented.

Actually there’s a little more, it looks like the benefit here is to layout the unit tests that you will fill in using a TDD approach and have the skeleton verified by your business stakeholders before you begin.  You can layout tests in the manner above without actually filling in the tests or any code and show the nicely formatted report about what you will test to your stakeholders and get agreement before moving forward.  Once you have business agreement on you MSpec setup you can then start codign by filling in the tests defined in each “IT” and writing code to make the tests  pass….interesting.

Now we’re going to start filling in some code ourselves.  Let’s see how this goes.

Just went over the notion of a “Row” test.  Apparently not supported in MSpec, but is supported in mbUnit and nUnit.  The idea is let’s you declare several different parameters for the same test so can write one test and run it with many different inputs.   Sounds like a really good way to cover my test cases with less code.

It looks like “Establish” delegate is used to demarcate a section of the test for setup.   Anything that needs to be setup for the “IT” tests to run below would be put here.  Phil suggests making all variables ‘internal’ and marking test projects friends so can set variable values for testing.  Phil went indicates he never marks any variables private, always internal so they can be accessed from a test.   Interesting…….

Beginning to go over mocking in a test driven development situation.  Phil’s using RhinoMock, whcih is what we use, so this is all making good sense to me.  Phil just setup an example showing how to inject the mock object into a method, it looks almost exactly like the process we use, so nice to see these experts telling people to do what we already do!  I like a little validation.

Going over the nitty gritty of using a mock object.  Specifically we’re talking about creating a wrapper for the file system so we can mock file system operations.  Interesting discussion, Phil suggests you only ever throw custom excpetions.  He suggests this because you can log in the creation of your custom exception and not worry about logging exceptions as they’re bubbled up.  Interesting theory.

Well presentation is winding down.  Very interesting, bdd stuff could be very beneficial from a process standpoint.  Also, again feel very good about what we are already doing after sitting through this.  Now have to figure out what to do for dinner!

Code Mash – Fundamentals Of Software Engineering

I’m sitting at Code Mash, in Sandusky, Ohio.   Right now I’m  listening to a presentation on Fundamentals of Sofware Engineering.  A little basic so far, given  by a gentleman by the name of Jon Kruger.   First part went over basics of OOP, encapsulation, inheritance and such.   Had a break now going over SOLID Principles and he’s basing it on Uncle Bob Martin’s book Agile Principles, Patterns and Practices.  One of my favorites and where I got my SOLID leanings from!

SOLID overview was a little confusing.  Presenter seemed to suggest to igore the principles until forced to not to, except he did seem to suggest to always follow DIP.  I  would disagree, I would suggest ignoring them until problems ocur makes it more likely for problems to ocur.  I would agree shouldn’t follow them blindly either, though.

Presenter is really high on Dependency Injection Containers.  Specifically he showed examples of StructureMap.  Looked useful, we rolled our own mostly because I wasn’t aware these existed when I did it.  I like to think it shows I had good instincts that I even thought to create dependency injection framework all on my own.  Probably should replace with one of these though.

Presentation is over, time for a  little hands on coding at the end of the session.  They’ve just haned out some requirements for a game and we’ve been given really bad code to try and refactor, or rebuild it from scratch.  Should be interesting.

Pair coding with my work buddy Ryan, trying to refactor/fix/update and ASP.net MVC project that implements two basic games.  We’ re quickly running out of time, struggling to figure out how to add 2 jokers into the deck for just one of the games!

Well we’ve decided to change direction and just confirm code meets the requirements we’ve been given.  We’ll fix and put in tests for deviations we find.  Clock is ticking!

Well that’s over.  Didn’t get a whole lot done on the coding task, not quite sure that it fit in well with the presentation, but maybe it’s just me.  Overall a pretty good presentation.  Mostly what I took from it was that we’ve been doing the right things on my current project.  That’s always good to hear.  I feel I contributed quite a lot to our current practices and approach and its nice to feel like we’ve done it right.

Tracing WCF Activity

In situations where we are setting up WCF clients calling a WCF service and are having problems getting the service call to work it is helpful to be able to see a trace of the communication. In order to do this add the below to the .config file of the service and a trace file named, ‘wcfDetailTrace.svcLog’ will be written out to the root folder of the service.

<system.diagnostics>
      <trace autoflush="true">
          <listeners>
          </listeners>
      </trace>
      <sources>
          <source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true">

    <listeners>
                   <add name="sdt"
                        type="System.Diagnostics.XmlWriterTraceListener"
                        initializeData= "WcfDetailTrace.svclog" />
               </listeners>
          </source>
      </sources>
 </system.diagnostics>

You can change the location and name of the trace file by changing hte ‘initializeData’ element of the ‘listener’. The trace file is verbose xml that can be parsed by the svcTrace.exe utitlity that comes with Visual Studio. If the svcTrace.exe utility is on your machine the extension .svclog will be mapped to it so double clicking on the trace file will open the trace file in svcTrace.exe.

Here is more information regarding using svctrace: http://msdn.microsoft.com/en-us/library/ms732023.aspx

If you want to log the content of each message then add the ‘MessageLogging’ elment to diagnostics underneath system.serviceModel as shown below to your config file. You will also have to add a source element for message logging to your sources element referencing “System.ServiceModel.MessageLogging”.

Continue reading “Tracing WCF Activity”

Subverting TeamCity

Every development environment I’ve worked on during the last few years used Cruise Control and Microsoft Visual Source Safe as a combination for source control and continuous build integration.  I’ve yet to come across a team actually using Microsoft’s Team Foundation Server.  I’m assuming the teams I’ve worked on weren’t large enough to warrant usage of this all encompassing tool.

What I have encountered is much criticism about Source Safe.  For years I’ve heard there are better source control alternatives, Subversion for instance.  This weekend I decided to give an alternate environment a chance and change my home build environment around.  I setup Subversion as my source code repository, and just for a change decided to use Jetbrain’s TeamCity product for continuous build integration.  Cruise Control has been fine, but why not see what else is out there? Continue reading “Subverting TeamCity”

And So It Begins

Having recently returned to the world of the itinerant .net Architect contracting world, I’ve decided to share my learnings and conundrums regarding the world of software development on the Microsoft Stack.  I hope to explore and work out issues regarding pragmatic unit testing and object orient design.  One thing I’ve learned through the years is that in software very little is right or wrong, but there are a whole lot of things that can make the experience better or worse.