Saturday, April 25, 2009

Geo Tracks – 2009 Apr 25

Hanselminutes 158 - Visiting Fog Creek Software and Joel Spolsky… I’m still not sure what exactly FogBugz is, but I learned a lot about how it was built and has evolved? Also, good to get some encouragement to keep writing, even if no one ever notices your blog :)

Psalm 80 – Psalm 85 ESV read by Max McLean

I started book 1, The Eye of the World, in Robert Jordan’s Wheel of Time series. Jeremy Miller put this at the top of his book list… I had never heard of it, so I’m giving it a try.  (Thanks for the iTunes gift cert Lucas!)

That’s it for this week… I worked from home Friday, so no commute.  Thursday I brought my wife to work with me so she could spend the day looking at places to live and schools in the area I work in… which means no podcasts since the wife likes to talk to me instead. :)  We may be moving closer to work soon!

NCharlie: TDD – part 3 (a UI test)

In this post I tackle the last task on my list for the task manager search feature: “Write tests, then implement the ShowSearchResults method.”

In part one I wrote unit tests to verify my TaskManagerController class is listening (subscribed) to the SearchRequested event, and that it correctly responds by calling the TaskManagerView class “ShowSearchResults” method.  In part two I verified that the TaskRepository correctly returns tasks when the Search method is called.

I admit: as I write this sentence I’m not sure how I will accomplish writing a test to verify that ShowSearchResults works… so I posted the question to Twitter:

I wrote a unit test to verify my controller calls my view's "ShowResult" method... What kind of test verifies ShowResult shows the result?

…and got a reply from my boot-camp instructor:

@charliesolomon we automate testing the ui. don't know that there's a great name for that other than automated ui test. or just ui test.

@mhinze So at this point (UI testing) it is time for me to use WatiN or equivalent?

@charliesolomon yes, sounds like it.

So here I go… WatiN is an open source web application test framework for .Net that will spin up an instance of my web browser and verify things on a web page.  To verify that ShowSearchResults is working I want to verify that the correct search results display on screen when the ShowSearchResults method is called on the TaskManagerView.

TaskManagerSearch_2

First I create a new class library project named UITests.  I want to keep these tests in a separate project because they take much longer to run than my unit tests.  If I’m going to keep this example strictly “Test First”, I need to, well, write my test first:

[Test]
public void Should_show_search_results()
{
Task task = new Task("foo");
TaskRepository repository =
new TaskRepository(new HybridSessionBuilder());
repository.Save(task);

using (IE ie = new IE("http://localhost:1035/Default.aspx"))
{
Button search = ie.Button(Find.ById("btnSearch"));
search.Click();

Div tasks = ie.Div(Find.ById("tasks"));

Assert.IsTrue(tasks.InnerHtml.IndexOf("foo") >= 0);
}
}


I’m not showing the [SetUp] method here for this Test Fixture… it ensures I have a clean database before running this test.  The test above just adds a task to the database, fires up the web page and verifies the search results come up correctly.



Default.aspx is a very simple page… just a text field, button and repeater to show the results. The “ShowSearchResults” method is what I’m interested in implementing and testing for this feature.  For the sake of brevity in this post, I will just refer you to the code to see how the ASPX page and code-behind file are constructed.  Here is the ShowSearchResults method from Default.aspx.cs:



public void ShowSearchResults(ICollection<Task> tasks)
{
rptTasks.DataSource = tasks;
rptTasks.DataBind();
}



I run my test and see WatiN fire up Internet Explorer, click the “Get” button, then search the results for my test record…



watin-clicking-get



watin-showing-results



…and the Resharper test runner shows the results:



ui-tests-passed



That’s it!  Of course it looks really simple here, and it is… but it actually took me 3 hours to get this simple test working.  Here’s why:



NUnit uses multi-threading by default, WatiN requires a single-threaded execution.  I solved this using a config file (“NCharlie.UITests.dll.config”… naming this config file correctly was actually the biggest time sink for this exercise.  I ended up adding a test to check that my config file was being read – thanks Charlie Poole)



For more information, check out the tdd-demo code branch and see the WatiN getting started guide.



<< NCharlie: TDD – part 2 (an integration test)  TDD Pain Points, A Retrospective >>

Saturday, April 18, 2009

NCharlie: TDD – part 2 (an integration test)

In my last post of this series I wrote unit tests to verify that an event (SearchRequested) from the View was handled by the Controller in my Agile/TDD sandbox application NCharlie.  I started with a sequence diagram showing the interactions needed to accomplish a search, then wrote down the tasks needed to complete this feature:

  1. Create interfaces for TaskManagerView and TaskRepository.
  2. Write tests, then write code to raise and handle the SearchRequestEvent method.
  3. Write tests, then implement the Search method.
  4. Write tests, then implement the ShowSearchResults method.

Two down, two to go.  The first two tasks could be test driven using unit tests.  Rhino Mocks allowed me to create a mock View and a mock Repository and concentrate on the TaskManagerController class… making sure it responded and handled the SearchRequested event correctly.  Now I want to wire up the Search method to return some actual results, and I want to show those results in an ASPX page, like this:

TaskManagerSearch

Since the Search method will be interacting with my database, I’ll need to test it using an integration test.  I like the way CodeCampServer keeps integration tests in a separate project… from the Resharper test runner it is easy to run all the tests in a project – so keeping the fast-executing unit tests separated seems to be a good idea.  I will add a new project to the NCharlie solution for integration tests and get to work (source is available in the tdd-demo branch of the NCharlie project.)

Here is the test method I want to build:

[Test]
public void Should_search_by_task_description()...


To write this test I will need to able to save Task entities to the database, retrieve them using the Search method, and make sure that the returned items contain the correct search results.  The saving part is accomplished in NCharlie using NHibernate and some helper classes that can be found in the open source projects CodeCampServer and Tarantino: IRepository, PersistentObject and HybridSessionBuilder.  These are mostly the same in NCharlie, though I did make a few changes so they would be .Net 2.0-friendly for this project.  The “PersistEntities” method below saves entities to the database, ensures the transaction is committed and the session is closed, so that subsequent calls to the database in this test can count on the entities being there.  I have not shown it here, but when running integration tests against a database it is important to setup the test first and make sure the database is in a consistent state.  This is done via a [SETUP] method in the RepositoryTestBase class, which my TaskRepositoryTester class is derived from.  Here is the test that verifies the search method is working correctly:



[Test]
public void Should_search_by_task_description()
{
Task task1 = new Task("foo");
Task task2 = new Task("xFOo1");
Task task3 = new Task("no fu");

PersistEntities(task1, task2, task3);

TaskSearchSpecification searchSpecification =
new TaskSearchSpecification();
searchSpecification.Description = "foo";

ITaskRepository repository =
new TaskRepository(new HybridSessionBuilder());
Task[] tasks = repository.Search(searchSpecification);

CollectionAssert.Contains(tasks, task1);
CollectionAssert.Contains(tasks, task2);
CollectionAssert.DoesNotContain(tasks, task3);
}


The “TaskSearchSpecification” class contains the search input fields (in this example I’m only searching on the Description field.)  Finally, I call the actual “Search” method on the TaskRepository and verify the results using the “CollectionAssert” class (part of NUnit.)



This test fails initially, and I implement the search method in TaskRepository to make it pass:



public Task[] Search(TaskSearchSpecification specification)
{
ICriteria criteria = GetSession()
.CreateCriteria(typeof (Task));

IList<Task> tasks = criteria
.Add(Restrictions
.InsensitiveLike("Description",
specification.Description,
MatchMode.Anywhere)
)
.List<Task>();

Task[] taskArray = new Task[tasks.Count];
tasks.CopyTo(taskArray, 0);

return taskArray;
}



task-search-test


For more information:



As I was writing this post I ran across Billy McCafferty’s article on NHibernate Best Practices with ASP.Net… I highly recommend it if you are interested in how these patterns are implemented.  I know things have changed in our field since he wrote this article (Billy later contributed the S#arp architecture which uses ASP.Net MVC), but this article is a great resource… especially for someone like me who works in a place where Webforms are still the tool we use to get our work done.  It is also very apparent from reading Billy’s article just how much it has influenced the ASP.Net world since it was written.  The CodeCampServer project I’m basing NCharlie on uses much of the architecture and patterns first described in Billy’s article and later in his S#harp architecture.



<< Previous NCharlie: TDD – part1, Next NCharlie: TDD – part 3 >>

Geo Tracks – 2009 Apr 18

Alt.Net Podcast #18, talking with Jeremy Miller about Alt.Net.  I like what Jeremy and James Avery have to say here.  My summary: Alties need to focus on improving their own craft instead of trying to force feed their current stack of tools/patterns/archs into the .Net mainstream.

Stack Overflow Podcast #47 – Joel and Jeff talk about sketching mockups with Balsamiq Mockups (a favorite of mine), say they will talk about Eclipse but really don’t, and keep ramblin’ on and on…

Hanselminutae 5 with Richard Campbell – Scott admits he is out to waste my time with this episode, but hey, I’ve got an hour and 15 to waste driving to work anyway!

SE Radio #129, F# with Luke Hoban – Good talk about use cases for F#.  Luke thinks the increase of parallelism in software as we keep adding cores to CPUs will make languages like F# much more needed.  Also interesting that F# targets the ad-hoc algorithm crowd… people who analyze data using tools like MathCAD or Mathematica.

Why People Believe What They Do, SciAm Podcast April 10, 2009 – Interesting discussion about a study on the correlation (or lack of) between education, religious affiliations, and beliefs in evolutionary theory and origins of life.  One general consensus was that humans naturally gravitate to solutions that show some sense of purpose vs. “it just happened randomly”.

Gospel Conference Part 5, Francis Chan – Francis starts with Ephesians 2 and the hope of heaven… also talks through some of the ways his church (Cornerstone Simi) cares for and rescues orphans, poor and abused people around the world.

Psalm 37 – Psalm 50 ESV read by Max McLean

Saturday, April 11, 2009

Geo Tracks – 2009 Apr 11

I spend a lot of time in the car each week.  This car, specifically (91 Geo Metro… 47 mpg!):

geo

I’m going to start linking to what I’m listening to during the commute each week (if I think they are worth sharing).  I’ll tag them “Geo Tracks”:

.NET Rocks #433: Phil Haack on ASP.NET MVC RTM!! Good explanation of MVC, and Supervising Controller / Passive Controller (MVP) models.

.NET Rocks #421: Derik Whittaker on nHibernate Good talk about use cases for nHibernate… but the best part of this show was Richard Campbell’s call out to the IT community (52:40) that the default monitor configuration for a developer should be 4960 by 1600 (30” in the middle, bookended by two 20” screens in portrait mode).  Yes please.

Stack Overflow Podcast #43 Joel and Jeff discuss how to handle incompetent programmers, among other things.  Specific other things include Joel whining about the service he receives everywhere he travels… It is getting more difficult to finish these each time I listen to this podcast, but I don’t think I’ll give up yet.  I was really impressed with the Podcast site.  First time I’ve visited, since I always update through iTunes.  They really keep good show notes and are liberal with the links to resources.

Francis Chan – 3/22/09 Living a Life that Matters Series: “Living with Joy” If you have never heard Francis speak, get ready to be convicted and changed.  Rejoice in the Lord… it is a command!

Scientific American PodcastIn Search of Time Journalist and writer Dan Falk talks about his new book In Search of Time, about the cultural, physical and psychological aspects of the mysterious ticking clocks all around us.

NCharlie: Test Driven Development – part 1

This is the second post in a series that tracks my progress implementing some of the technologies, patterns and methods I picked up at an Agile .Net boot-camp in March 2009.

The exercise for this post is to strip down my sandbox project, NCharlie, to a point where I can really see the benefit of test-first development.  Then I will add a feature to the project using the process my instructor followed (“User Story Execution Process”, described here).  You can download the source for this exercise by browsing the tdd-demo branch of the NCharlie project on Google Code.

The project is a simple task management tool.  The first feature I’ll implement is the search feature.  Here’s the requirement:

“A user should be able to search for existing tasks.”

I know, that is a pretty vague requirement.  I’ll add on more layers later… for now, this requirement will serve nicely (I hope) in demonstrating the TDD method.  I spent some time setting up my “Core” project which contains the domain model (for now, just one entity… a Task).  Next I create a sequence diagram describing a search for existing tasks:

task-manager-search-sequence

Aside: If you are staring at this diagram wondering why I’m using Views, Controllers and Repositories, it is because of the architecture decisions I’ve made for this project.  I hinted at the motivation for this architecture in the first post of this series… I may come back to that in more detail later.  For now though I will just admit that it was pretty easy to think about the ingredients for this sequence diagram because I had already built this and now I’m just starting over.  When I built NCharlie initially there was some pain I had to work through getting the parts of the Supervising Controller pattern to work in the context of a webforms application using the Onion Architecture.  But that is figured out now, and I won’t have to figure it out again on every application.  I know now that I need to model View events and handle those events in a Controller.  The Repository pattern is used to abstract away the persistence (database) part of the application. (end of Aside)

Back to the exercise… I’ll break the elements of the sequence diagram down into development tasks:

  1. Create interfaces for TaskManagerView and TaskRepository.
  2. Write tests, then write code to raise and handle the SearchRequestEvent method.
  3. Write tests, then implement the Search method.
  4. Write tests, then implement the ShowSearchResults method.

Step 1: Here are the two interfaces:

public interface ITaskManagerView : IView
{
event EventHandler<TaskSearchEventArgs>
SearchRequested;
void ShowSearchResults(ICollection<Task>);
}



public interface ITaskRepository : IRepository<Task>
{
Task[] Search(
TaskSearchSpecification specification);
}



The view interface is derived from the IView interface, which I won’t explain in detail here.  Phil Haack did a good job of explaining this in his Supervising Controller example – IView holds common event handlers and methods for a web form (Init, Load, IsPostBack…)



The repository interface is derived from IRepository, which holds CRUD methods used to persist objects.  I added the Search method to ITaskRepository.



Step 2: The SearchRequestEvent:



For this step, I need to write 2 tests.  First, the Controller must listen (subscribe to) the View event so that it can respond.



[Test]
public void Verify_controller_attaches_to_search_requested_event()...



Second, I want to make sure the Controller calls the Search method on the repository when this event is raised.



[Test]
public void Repository_search_method_should_be_called_in_response_to_search_request()...



You may be asking why I left the guts of these tests out of this post?  After all, this is supposed to be a post about writing tests first.  I struggled with this for a while and decided not to show the implementation here because I don’t want to get sidetracked trying to explain how to create mock calls to the view and to the repository.  One of the benefits of writing these tests is that I can do so before I create any ASPX pages or database procedures.  Before I even have to think about ASPX or database connections and queries I can have a compiling project with unit tests that specify the behavior I want to achieve.  That is the value I hope to convey in this post.  The test implementation details are available in the source for this exercise, and I hope to cover it in more detail in a later post.



After implementing the above two tests correctly my project compiles, but when I execute the tests they fail (I’m using the Resharper test runner to execute my test methods.  At boot-camp we used TestDriven.Net.  You can also execute these tests from NUnit’s console runner or the command line if you like.):



SearchRequestedEvent tests failed



Now I’ll write the code to make these two tests pass.  I create an event handler method (OnSearchRequested) that calls the repository’s Search method and attach it to the View’s SearchRequested event:



public class TaskManagerController
{
private readonly ITaskManagerView _view;
private readonly ITaskRepository _repository;

public TaskManagerController(
ITaskManagerView view,
ITaskRepository repository)
{
this._view = view;
this._repository = repository;
SubscribeViewToEvents();
}

private void SubscribeViewToEvents()
{
_view.SearchRequested += OnSearchRequested;
}

private void OnSearchRequested(
object sender,
TaskSearchEventArgs e)
{
ICollection<Task> tasks =
_repository.Search(e.SearchSpecification);
_view.ShowSearchResults(tasks);
}

}



Now I rebuild and execute my unit tests:



SearchRequestedEvent tests pass



These two unit tests are passing now, and they don’t require an ASPX page or a database to do so.  I think that is valuable!  They clearly describe what the behavior of this part of my application should be and they enforce that behavior without requiring me to think about the specific implementation of the web form or the repository.



Next time I will continue with steps 3 and 4 in my task list above... testing and implementing the Search and ShowSearchResults methods.



<< Development Driven Tests (Introduction), NCharlie: Test Driven Development, part 2 >>

Sunday, April 5, 2009

Development Driven Tests

I’ve got the title wrong backwards to make a point.  That point is coming…

I attended .Net boot-camp a few weeks ago and practiced Test Driven Development.  We talked about new features to add to CodeCampServer (the open source project used as the coding sandbox for class).  Our instructor drew sequence diagrams on the whiteboard and we designed the interfaces and interactions the features would require.  We assigned tasks, wrote tests that failed, then wrote code to make them pass.  It was great.  To me it felt the way development should feel, like we were doing it right.

Now I’m trying to work through how to take these methods, which feel so right, back to work.  It seems to me this is a common problem developers like me deal with, and I want to succeed.  So I’ve been working through a project that uses the agile tools and methods demonstrated at boot-camp and tries to flesh them out using code that will run in our environment at work.  My thought was that a finished project, built using the methods I want to use more at work, would go a long way to convincing my coworkers of the value these methods and tools could bring.  Here are the ingredients I’m throwing together in my example project:

  • Onion Architecture (to remove some pain caused by tightly coupled areas of our applications)
  • Test First Development (using NUnit and Rhino Mocks, to realize the increased code quality and code longevity that TDD proponents promise)
  • Object-Relational-Mapping (using NHibernate, because I’m tired of maintaining dozens (hundreds?) of stored procedures per app)
  • Visual Studio 2005 (because that’s what we use at work)
  • Web Forms using the Supervising Controller pattern (because we can’t use MVC at work and this seemed to me a testable next best thing for web forms)

I’ve named my project “NCharlie” for now, since it uses at least 3 technologies that start with “N”, and since it will help Charlie (me) chronicle my journey towards becoming a better programmer.  It is a simple task management tool (add to-do items, assign tags to them, search for them).

This brings me back to the title of this post, and my point.  After spending a few hours on NCharlie I’m realizing that it is hard to make the transition to TDD.  I have been making the software work first for so many years now that it feels unnatural to write tests before implementations.  I am now at the point where I am writing more tests… but NCharlie is already working!!  I fell hard back into my old pattern and implemented the design before I wrote tests.  Sure I wrote a few tests up front, but I have to be honest with myself and admit they were only copies of tests I learned about at boot-camp.  Anything specific to the design of my test project has been just plain hard for me to test first.

So is NCharlie just going to end up on my FAIL list?  I hope not… I still have plans for it (actually it is the reason I’m starting this blog.)  The way it sits now NCharlie is of no use to me or my coworkers because it doesn’t prove what I set out to prove… in fact it disproves it! 

This is a wake up call for me, because I think a developer who excels at writing tests first must also have a good handle on other principles/patterns that distinguish an average developer from a master.  So I’m average, that’s ok.  But I don’t want to stay here.  I have been reading books and blogs for a couple years now that describe pragmatic and agile methods I know I want to use in my career as a developer.  I’ve been successful with some, but the more I learn the more I realize how far I have to go.

So here I go… new plan.  I’m going to dissect NCharlie and show you its ugly guts in public.  I have no shame.  In fact I’m quite proud of some of it… even though it is blatantly not original.  It uses code from open source projects like CodeCampServer and Tarantino.  I can however, take credit for getting them all working together from “File, New Project”.

So let’s call this an introduction to a series of posts.  Mainly I want to document the process for my team at work and for myself so we can use this as we hopefully start to integrate these methods into our workflow (if I can get NCharlie to the point of proving its usefulness).  In the next post I’ll attempt to strip down NCharlie and start over at the point where TDD actually shows some benefit.

Next in series -  NCharlie: Test Driven Development – part 1 >>