Skip to content

Instantly share code, notes, and snippets.

@MichaelSitter
Created May 11, 2012 00:07
Show Gist options
  • Save MichaelSitter/2656712 to your computer and use it in GitHub Desktop.
Save MichaelSitter/2656712 to your computer and use it in GitHub Desktop.
Testing in Nancy is so pretty!
[TestFixture]
public class EntityModuleTests
{
private static IEntityDataService fakeDataService;
private ConfigurableBootstrapper bootstrapper;
private Browser browser;
[SetUp]
public void Setup()
{
fakeDataService = A.Fake<IEntityDataService>();
bootstrapper = new ConfigurableBootstrapper(configuration => GetConfiguration(configuration));
browser = new Browser(bootstrapper);
}
private static ConfigurableBootstrapper.ConfigurableBoostrapperConfigurator GetConfiguration(ConfigurableBootstrapper.ConfigurableBoostrapperConfigurator configuration)
{
configuration.Modules(new[] { typeof(EntityModule) });
configuration.Dependency<IEntityDataService>(fakeDataService);
return configuration;
}
[Test]
public void GetEntities_WhenEntitiesExist_ShouldReturnOK()
{
A.CallTo(() => fakeDataService.GetEntities()).Returns(new List<EntityResource>
{
new EntityResource(),
});
var result = browser.Get("/entities");
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
}
[Test]
public void GetEntity_WhenEntityExists_ShouldReturnOK()
{
var entity = new EntityResource
{
Id = 1,
Description = "Description",
};
A.CallTo(() => fakeDataService.GetEntity(A<int>.Ignored)).Returns(entity);
var result = browser.Get("/entity/1");
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.That(result.Body.DeserializeJson<EntityResource>().Id, Is.EqualTo(entity.Id));
Assert.That(result.Body.DeserializeJson<EntityResource>().Description, Is.EqualTo(entity.Description));
}
[Test]
public void GetEntity_WhenEntityDoesNotExist_ShouldReturnNotFound()
{
A.CallTo(() => fakeDataService.GetEntity(A<int>.Ignored)).Returns(null);
var result = browser.Get("/entity/1");
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}
[Test]
public void CreateEntity_WithValidData_ShouldReturnCreated()
{
var createdEntityId = 5;
A.CallTo(() => fakeDataService.CreateEntity(A<EntityResource>.Ignored)).Returns(createdEntityId);
var result = browser.Post("/entity/");
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.Created));
Assert.That(result.Body.DeserializeJson<int>(), Is.EqualTo(createdEntityId));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment