Hi there,
I am writing a small app to teach myself ASP.NET MVC, and one of its features is the ability to search for books at Amazon (or other sites) and add them to a "bookshelf".
So I created an interface called IBookSearch (with a method DoSearch), and an implementation AmazonSearch that looks like this
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)
{
//Get values inside an XElement
}
}
Ideally I would like to have done this TDD-style, writing first a test and all. But I gotta confess I am having trouble getting my head around it.
I could create a FakeSearch that implements DoSearch() and return some ad-hoc books, but I don't think that brings any value at the moment, does it? Maybe later when I have some code that uses a list of books.
What else could I test first? The only test I can think of would be one that mocks the call to the cloud (at GetAmazonResults) and then checks that DoSearch can execute the Linq2XML select correctly and return the correct list. But it seems to me that this type of test can only be written after I have some code in place so I know what to mock.
Any advice on how you guys and girls would go around doing this test-first style?