I am writing a small application to teach ASP.NET MVC, and one of its features is the ability to search for books on Amazon (or other sites) and add them to the bookshelf.
So, I created an IBookSearch interface (with DoSearch method) and an AmazonSearch implementation that looks like
public class AmazonSearch : IBookSearch { public IEnumerable<Book> DoSearch(string searchTerms) { var amazonResults = GetAmazonResults(searchTerms); XNamespace ns = "http://webservices.amazon.com/AWSECommerceService/2005-10-05"; var books= from item in amazonResults.Elements(ns + "Items").Elements(ns + "Item") select new Book { ASIN = GetValue(ns, item, "ASIN"), Title = GetValue(ns, item, "Title"), Author = GetValue(ns, item, "Author"), DetailURL = GetValue(ns, item, "DetailPageURL") }; return books.ToList(); } private static XElement GetAmazonResults(string searchTerms) { const string AWSKey = "MY AWS KEY"; string encodedTerms = HttpUtility.UrlPathEncode(searchTerms); string url = string.Format("<AMAZONSEARCHURL>{0}{1}",AWSKey, encodedTerms); return XElement.Load(url); } private static string GetValue(XNamespace ns, XElement item, string elementName) {
Ideally, I would like to make this TDD style by writing a test first and thatโs it. But I have to admit that I am having problems with my head.
I could create a FakeSearch that implements DoSearch () and will return some special books, but I don't think this brings any value at the moment, does it? Maybe later, when I have code that uses a list of books.
What else could I check first? The only test I can come up with is one that makes fun of a call in the cloud (in GetAmazonResults), and then checks that DoSearch can correctly execute Linq2XML and return the correct list. But it seems to me that this type of test can only be written after I have the code in place, so I know what to make fun of.
Any tips on how you guys and girls will get around this first test?