Outside in TDD clarifications


If you have been trying to follow the tutorial on Outside in TDD by Mark Seemann but don’t have access to the source code, then you will have probably come across certain issues.
Here I will detail what I did differently to him and also how to overcome some of the issues.

HttpClient

In the videos, the author makes use of the “microsoft.net.http” dlls but I believe that libraries are now available in “System.Net.Http”. However, if you decide to choose the latter, you might come unstuck while using the HttpClientFactory as the latter is part of “System.Net.Http”. I didn’t delve into it as I’m sure it provides a way to create the client and so I simply created a “MyHttpClientFactory” to get round this issue.

PostAsJsonAsync

This method is part of System.Threading.Tasks.

GetAfterPostReturnsCorrectStatusCodeWithPostedEntry

This is one of the tests that he uses and at some point he comes up with code as follows:

At this point you get two problems:

  1. ToJObject()
  2. ReadAsJsonAsync()

The second issue is “easy” to deal with because from the video you can see a class called “ContentReader.cs”. The latter so happens to be available here:

https://github.com/ploeh/RunningJournalApi/blob/master/RunningJournalApi.AcceptanceTests/ContentReader.cs

part of which is reproduced below for your convenience:

The first issue was slightly harder to figure out because I am not familiar enough with Json.NET but eventually using the code in GitHub as given above, it was possible to resolve the issue. Hence the code is now:

You could of course tidy up the code in order to create that method “ToJObject” but I will leave that to the reader to do this.

Claim

The “Claim” class is part of System.Security.Claims but that namespace is only available as from .NET 4.5.

ReadAsAsync

You will need to reference System.Net.Formatting in order to get this working (http://stackoverflow.com/questions/10399324/where-is-httpcontent-readasasync).

Advertisements
Posted in .NET, TDD
2 comments on “Outside in TDD clarifications
  1. Chris Gomez says:

    Thank you for posting this. I am still in progress on following the code on my own. Here is what I found.

    HttpClientFactory:
    At 0:15 of Demo:Posting an entry – Mark shows the HttpClientFactory helper class he created. This easy to confuse with System.Net.Http.HttpClientFactory, and refactoring tools will want to use that until you create your own in the same namespace.

    Install-Package Microsoft.AspNet.WebApi.SelfHost – This is a deprecated package. The guidance is now to use the OWIN Self Host. However, I was able to proceed with this legacy package as Mark installs it.

    PostAsJsonAsync:
    This is an extension method in System.Net.Http.Formatting.dll. I didn’t find I had to do anything special here. I believe it returns a Task but isn’t part of the System.Threading.Tasks namespace.

    ToJObject:
    This was pretty perplexing. It looks like what happens is Mark uses an anonymous type to quickly make an object that the json formatter will easily gobble up in PostAsJsonAsync. However, you have an anonymous typed object that you can’t compare to the deserialized json objects in entries. So I’m guessing ToJObject does nothing more than serialize and then deserialize using Json.NET. So I created a ToJObject extension method:

    public static dynamic ToJObject(this T obj)
    {
    dynamic serialized = JsonConvert.SerializeObject(obj);
    return JsonConvert.DeserializeObject(serialized);
    }

    and used it this way to save the expected value for the assertion:
    var expected = json.ToJObject();

    Don’t know if this is better but thought I would share.

    ReadAsJsonAsync:
    The extension method you posted works well and it DOES require a using to System.Threading.Tasks (shouldn’t be a big deal as this is in mscorlib

  2. j says:

    Thank you for your notes, I came accross same issues and Mark never mentions details like this that causes trouble…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: