This is my first screencast in a planned series of videos that will demonstrate and discuss some unit testing and test driven development practices that I have found to be useful. In this video, we verify that a RESTful client is communicating with a webserver by redirecting it to a hosted NancyFX server under our control.
Category Archives: Uncategorized
RESTful Client Unit Testing with NancyFX
Image courtesy of Scott Shiller
Over the past week or so, I’ve been working on some code that will query a RESTful service. As I was writing this code, I separated the network communication from the processing of the content. In my mind, this is a good isolation of the two concerns. The challenge that I faced, was how do I test the communication with these external services?
I am a unit testing fanatic. I have had horror stories of getting to the final few days of a project and one of my quality assurance analysts informing me that a bug was found in a block of code that wasn’t directly touched. I’ll go into my thoughts on TDD, BDD, and the other testing practices in another post…
How should I test a block of code that is dependent on a third party resource? Isolate the resource, and ‘mock’ it. Typically, this approach is handled through a Mocking library. Personally, I prefer the Moq library… but in this case, Moq isn’t going to completely handle my problem.
The following code has been sanitized to protect the innocent. Consider the following simple classes to request data from the service:
/// /// A simple DTO /// public class MyOrder { public int Id; public string CustomerName; public decimal OrderTotal; } public class ClientConnector { public Uri ServiceLocation = new Uri("http://api.samplerestfulservice.com"); public IEnumerable GetOrders() { var req = WebRequest.Create(ServiceLocation); WebResponse resp = null; resp = req.GetResponse(); var js = new JavaScriptSerializer(); var sr = new StreamReader(resp.GetResponseStream()); var orders = js.Deserialize(sr.ReadToEnd()); return orders; } }
Note the referenced service location on line 16. This is where we can start injecting our Mock service.
I have used the NancyFX web server in the past for small projects where I needed to host a WebUI inside of an application as a ‘remote console’ for an administrator. In this case, we can use the Nancy service as a mock host for our unit tests to demonstrate that the service is properly communicating with the service provider.
In a unit test project, we can add the following NuGet references to get started:
Now we can start a TestFixture class with a Nancy service started and maintained for the duration of the fixture:
[TestFixture] public class SimpleFixture { private NancyHost _Nancy; protected Uri _MyUri = new Uri("http://localhost:50001"); [TestFixtureSetUp] public void FixtureSetup() { _Nancy = new NancyHost(_MyUri); // Need to retry in order to ensure that we properly startup after any failures for (var i = 0; i < 2; i++) { try { _Nancy.Start(); break; } catch { try { _Nancy.Stop(); } catch (Exception e) { Console.Out.WriteLine("Exception setting up: " + e); } } } // Prime the connection var req = WebRequest.Create(_MyUri); req.GetResponse(); } [TestFixtureTearDown] public void FixtureTeardown() { try { _Nancy.Stop(); _Nancy = null; } catch { } } public class StubModule : NancyModule { public static bool _Connected = false; public StubModule() { Get["/"] = _ => { _Connected = true; return "[]"; }; } } }
Check out line 7, this is where I have defined the base URI for the Nancy webserver to listen on. It is important that the port to be listened on be in the 50000 range, as these network ports are all marked for private use.
In Nancy, we write NancyModule classes that define how to respond to Http Requests. The StubModule written in this code defines a single route to return data on. At line 60 you’ll see where I have configured this initial Get request handler to return a pair of brackets. For this sample service, it is expecting an array of JavaScript objects to be returned, so the square brackets in the StubModule indicate an empty JavaScript array. To wire-up a test for my simple method, it might look something like this:
[Test] public void WhenNoOrdersAreAvailableShouldReturnEmptyOrdersCollection() { // Arrange var connector = new MyRestClient.ClientConnector { ServiceLocation = _MyUri }; // Act var result = connector.GetOrders(); // Assert Assert.IsTrue(StubModule._Connected, "Did not request data from the service"); Assert.IsNotNull(result, "Did not return data from Connector - at a minimum we should get an Empty collection"); Assert.AreEqual(0, result.Count(), "Returned items in the array, when none were transmitted"); }
Now I have a simple test that can be used to verify that my RESTful client made a connection to the Nancy service and returned successfully. Other tests with mocked responses from Nancy can be written and stubbed results can be further defined in the StubModule.
Vermont.Net Wrap-up
On Monday, June 18th I made the trek from my home near Philadelphia to Burlington, Vermont for a speaking engagement with the Vermont.Net user group. This was my first speaking engagement for a user group outside of the Philadelphia area, and I must admit: I was a tad nervous about the trip.
I’m not one who enjoys driving for long trips. If I drove straight through to Burlington, it would have been a 7 hour drive… yuck. I worked out that I could drive to New York for a JetBlue flight to Vermont. That may sound strange, but it cut my travel time by two hours, gave me some rest after driving and my expenses were going to be covered by INETA. With the speaker travel reimbursement offer from them, I just about broke even on the travel costs.
My drive to the JFK airport looked like it would be uneventful, as I left after morning rush-hour in the Philadelphia area and crossed into New Jersey with good speed, and no traffic. I pulled into a rest stop on the Jersey Turnpike to grab some snacks before lunch near JFK, and departed to find myself stopped not 5 miles further down the road. Fire trucks went screaming down the shoulder and traffic started moving about 10 minutes later. The culprit: a car had caught on fire in the middle of the road. I have never seen a vehicle spontaneously-combust, but in Jersey, I guess these things happen. The rest of the drive to JFK was uneventful and I found the Jetblue personnel to be very friendly and helpful, a blessing for a first time traveler on their airline.
When I arrived in Vermont, I was a bit surprised at how small the Burlington airport was… all of my prior flights had been to larger cities: Boston, Detroit, Phoenix, Dallas, Orlando, Atlanta… you get the picture. I was greeted by Matt Bean from the Vermont.Net user group and we went for a quick walking tour through Burlington.
The event started at 6:30 at mywebgrocer.com’s offices in Winooski. The venue was an interesting setup as I had a projector hooked up to my laptop and alternate video split to two large LCD displays that were facing me. This was a big help to me, as I have been ‘accused’ of referring to my slides and speaking towards them too much when I present.
I was the first of two speakers on this evening, and presented my ‘Introduction to CQRS’ talk. For those that have not seen it, I discuss the differences between a standard n-tier architecture and a CQRS/DDD architecture in a web application. I use a modified version of the NerdDinner website to demonstrate how a domain object is constructed, task-based-ui can be implemented, and how events are stored and are then available for an ‘audit-log’ type of presentation. The group was great, and had lots of feedback and questions. As this was an introductory talk, I led them to the sample code of NerdDinner I had posted to BitBucket for more experimenting after the event.
The second speaker was the accomplished and very impressive Glenn Block. Glenn spoke for an hour on WebAPI, and opened my eyes to some of the easier ways to reliably have machines communicate with each other over standard web ports. I’ll be considering how best to use WebAPI in my next project for my employer…
The real experiences that I gained from this trip was the discussions after the meeting. An unexpected visitor in the form of Darrel Miller who came in from Montreal to talk a bit about REST services. After some discussion between Glenn Darrel and myself, I was impressed with the depth and direction that REST services are headed. While I initially entered the discussion with the feeling the REST was just another way to communicate with a webserver, I learned that there was a lot more to it. I definitely need to spend more time researching content types and header records that are used in requests to RESTful services, as this is a communication strategy I planned to use in my next sprint.
Finally, I got to spend the second day of my trip with the amazing Julie Lerman. Julie is the author of several books on the Entity Framework, and I had a full day to enjoy with this brilliant technologist. What a good thing for me as I spent the previous 6 months leading a project that uses Entity Framework as the core data access technology. We spent the day hiking, chatting, and enjoying the amazing views and mountains of Vermont. I was able to ask several questions that had been dogging my team, and got some great insight as to how my team should address our issues in a future sprint. Many thanks to Julie for her hospitality during my visit.
All in all, a great trip to Vermont. I learned a lot from a group of great technologists who were visiting the area for the evening. For a first trip outside of my geographic comfort zone, it was very encouraging to me. I now have some confidence that I can do more of these trips over the coming months.
I’ll be putting together talks on Windows 8 development with Javascript, QUnit-Metro development, and perhaps an Asp.Net MVC 4 talk as well. Look for me to visit New York, Virginia, Harrisburg, Allentown, and a visit or two with my favorite Philly.Net folks as well.
Here is my presentation from the finals of the Speaker Idol competition at Tech Ed 2012.
My Tech Ed NA 2012 Speaker Idol wrap-up
I’m finally coming down off of the emotional high that I had coming home from Tech Ed NA 2012 in Orlando. I had a very long week, met a lot of great people that I only knew by their twitter account or blog postings, and saw some interesting content.
My first day started off with a bang, as I was entered into the ‘Speaker Idol’ competition. This was a VERY important event for me, as I have been aspiring to present at a Tech Ed conference for years. The Speaker Idol competition is a talent contest in the same vein as American Idol: competitors give a brief presentation each of the first three days. The three winners and a wild card from the 3 second place entrants go to the finals. The winner of the finals on Thursday gets a full invite to Tech Ed 2013 to present a full session.
I was the third to present on the first day, and consequently the pressure was on from the moment I stepped off of the plane. I attended the first day’s keynote that covered Microsoft System’s Center, and was wholly unimpressed. As a developer, I could see that I would be drowning in IT content for the week.
I skipped the second session of the day so that I could focus on practicing and being ready for Speaker Idol after lunch. When the competition started, I watched the first two entrants and kept my confidence about me, as I was about to give a presentation that had content no one could compare against.
My presentation introduced a unit testing framework for JavaScript Metro applications in Windows 8. I was demonstrating code in a new operating system, built with a new development tool, with a new open-source project that I announced on stage. Yea.. content doesn’t get any fresher than THAT.
I took my lumps from the judges and was awarded second place on the day. I knew it would be up to me at that point to attend the other 2 preliminary rounds and pray that the second place finishers did not stand up to the quality presentation I started the week with.
I attended several very good sessions on using Visual Studio 2012 for testing, application life-cycle management, and how to get more out of the tool with shortcuts and hot keys. I even attended a few sessions on Windows Azure architecture and service bus use.
As part of my volunteerism with INETA, I assisted in two ‘Birds-Of-A-Feather’ sessions. For the uninitiated, these are group discussion sessions with experts to assist in advancing the discussion. I assisted with a session on ‘Local Developer Community’ that was sparsely attended and a session on ‘Better Scrum’ that had about a 75% room attendance rate. Both were interesting topics, and it was great to assist on a session at the conference, as I walked away feeling like I (in some very small way) contributed.
On Wednesday afternoon, I received the message from (Speaker Idol host) Richard Campbell that I was to be the wild card entrant into the finals of Speaker Idol. From that point, I knew I had nothing to lose. I attended a final session that afternoon, and spent a chunk of the evening with colleagues re-writing parts of my presentation to comply with the judges criticisms from earlier in the conference.
On Thursday at lunch, I got the draw and it was determined that I would be the last speaker. This was a golden opportunity for me, as I could make the lasting impressions with the judges when they go into their final deliberation. I gave my presentation, and the judges commentary came down.
I was quite disappointed that the changes that I made to meet their comments were met with exactly the opposite commentary than what they delivered in the initial round. For example: I removed a faux demo failure in the talk and replaced it with a sample that explicitly demonstrated a failing test scenario. The judges felt that this demo was bland, and did not have any excitement, where previously they were too excited and distracted by my demo.
I will finish posting these two videos from the finals in the coming day or two. I’ve been a bit distracted since coming home.
Unfortunately, I did not place in the finals, and David Giard was named the ‘Speaker Idol’ David is a great speaker, and I’m happy for him. I found it a bit odd that there was no ceremonial ‘green shirt’ to bestow on him as a ‘prize’ for winning. Ah well…
My mission was achieved, I attended a phenomenal Tech Ed. I got myself a good amount of press based on my appearance at Speaker Idol. I met and spoke with a number of Microsoft employees, MVPs, and great community members. With a little bit of hard work, luck, and a successful QUnit-Metro project over the next 2-3 months and I think I have a shot at an MVP award.
We’ll see what happens.
Me with the Speaker Idol and DotNetRocks hosts: Richard Campbell and Carl Franklin. Our paths WILL cross again 😉