Skip to content

Test Driven Development Yes! This library supports it!

Pure Krome edited this page Jul 17, 2013 · 3 revisions

NOTE: THIS DOCUMENT IS NOW OUT OF DATE AS OF 17/07/2013

Yes! This library supports test driven development - because everyone knows that we all should be doing TDD with all our .NET applications.

A common scenario of this would be to test your ASP.NET MVC controller methods or your NancyFX modules.

There are two ways you can use this library while you practice TDD.

  1. Simple 99% 'ers - Use the Fake providers.
  2. Hardcore 1% 'ers - Mock out the IRestClient and setup the Methods you wish to mock.

Simple 99% 'ers - Use the Fake Providers.

// Arrange.
var authenticationService = new AuthenticationService();
authenticationService.AddProvider(new FakeFacebookProvider(
    new Uri("http://localhost:1338/home/AuthenticateCallback?providerKey=facebook"));

var homeController = new HomeController(authenticationService);

// Act.
var result = homeController.AuthenticateCallback("facebook");

// Assert.
Assert.NotNull(result);
// .. etc..

or an error occurs, like the user doesn't accept the providers accept policy.

// Arrange.
var authenticationService = new AuthenticationService();
var facebookProvider =
    new FakeFacebookProvider(new Uri("http://localhost:1338/home/AuthenticateCallbackThatErrors?providerKey=facebook"))
    {
        AuthenticateCallbackExceptionMessage =
            "ZOMG! Something nasty has occured! ID10T Error!1!1!1. -le sad panda-"
    };
authenticationService.AddProvider(facebookProvider);
var homeController = new HomeController(authenticationService);

// Act.
var result = Assert.Throws<AuthenticationException>( () => homeController.AuthenticateCallback("facebook"));

// Assert.
Assert.NotNull(result);
// .. etc..

Hardcore 1% 'ers - Mock out the IRestClient

This is a lot more complex, but gives you finer granular control of various success/failing scenarios. Please look at the extensive unit tests for examples about how this is achieved.