Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nesting test fixtures using NUnit, about as close to nested Jasmine tests for non-async code
using System;
using System.Collections.Specialized;
using Once.Api.Core;
using Once.Api.DataAccess.Query;
using Once.Api.Domain;
using Once.Api.Endpoints.Factory;
using Once.Api.Endpoints.GetWidget;
using Once.Api.Endpoints.Serialisers;
using Once.Api.Endpoints.Validators;
using Acme.Model;
using Moq;
using NUnit.Framework;
using Ploeh.AutoFixture;
using ServiceStack.Web;
namespace Once.Api.Endpoints.Tests.Unit.GetWidget
{
[TestFixture]
public class GivenAGetWidgetRequestHandler : AutoFixtureInstance
{
protected Mock<ISprocketValidator> MockSprocketValidator;
protected Guid SessionId;
protected Mock<ISprocketByIdQuery> MockSprocketByIdQuery;
protected CrumblySprocket ExpectedCrumblySprocket;
protected Mock<IWidgetFactory> MockWidgetFactory;
protected Mock<IAcmeSerialiser> MockAcmeSerialiser;
protected string ActualResponse;
protected string ExpectedResponse;
protected GetWidgetRequestHandler GetWidgetRequestHandler;
protected GetWidgetHandlerContext Context;
[OneTimeSetUp]
public void BeforeAll()
{
SessionId = Fixture.Create<Guid>();
ExpectedResponse = Fixture.Create<string>();
Context = new GetWidgetHandlerContext
{
Response = Fixture.Create<IResponse>(),
ResponseContentType = Fixture.Create<string>(),
SessionId = SessionId
};
ExpectedCrumblySprocket = Fixture.Create<CrumblySprocket>();
MockSprocketValidator = new Mock<ISprocketValidator>();
MockSprocketValidator.Setup(
validator => validator.ValidateOpenFaultSprocket(It.IsAny<Guid>(), It.IsAny<Guid>()))
.Returns(ValidationResult.Success);
MockSprocketByIdQuery = new Mock<ISprocketByIdQuery>();
MockSprocketByIdQuery
.Setup(query => query.OpenFaultSprocketExists(It.IsAny<int>()))
.Returns(true);
MockSprocketByIdQuery
.Setup(query => query.GetOpenFaultSprocket(It.IsAny<int>()))
.Returns(ExpectedCrumblySprocket);
MockWidgetFactory = new Mock<IWidgetFactory>();
MockWidgetFactory.Setup(factory => factory.Build(It.IsAny<CrumblySprocket>()))
.Returns(new Widget());
MockAcmeSerialiser = new Mock<IAcmeSerialiser>();
MockAcmeSerialiser.Setup(
serialiser => serialiser.SerialiseAcmeMessage(It.IsAny<string>(), It.IsAny<Widget>()))
.Returns(ExpectedResponse);
var mockConfigurationManagerWrapper = new Mock<IConfigurationManagerWrapper>();
mockConfigurationManagerWrapper
.Setup(wrapper => wrapper.AppSettings)
.Returns(new NameValueCollection { { "IntegrationApiSystem", "Crumbly" } });
GetWidgetRequestHandler = new GetWidgetRequestHandler(
MockSprocketValidator.Object, MockSprocketByIdQuery.Object, MockWidgetFactory.Object,
MockAcmeSerialiser.Object, mockConfigurationManagerWrapper.Object);
}
[TestFixture]
public class WhenCallingGet : GivenAGetWidgetRequestHandler
{
[TestFixture]
public class AndTheQueryStringIsNotValid : WhenCallingGet
{
[Test]
public void AndNoPipeThenThrowsFormatException()
{
Context.GetWidgetRequest = new GetWidgetRequest
{
Identifier = $"urn:Crumbly{Fixture.Create<int>()}"
};
Assert.Throws<FormatException>(() => GetWidgetRequestHandler.Get(Context));
}
[Test]
public void AndNoUrnThenThrowsFormatException()
{
Context.GetWidgetRequest = new GetWidgetRequest
{
Identifier = $"Crumbly|{Fixture.Create<int>()}"
};
Assert.Throws<FormatException>(() => GetWidgetRequestHandler.Get(Context));
}
[Test]
public void AndNotCrumblyThenThrowsFormatException()
{
Context.GetWidgetRequest = new GetWidgetRequest
{
Identifier = $"urn:Partnumber|{Fixture.Create<int>()}"
};
Assert.Throws<FormatException>(() => GetWidgetRequestHandler.Get(Context));
}
}
[TestFixture]
public class AndTheQueryStringIsWellFormed : WhenCallingGet
{
[TestFixture]
public class AndOpenFaultSprocketExistsReturnsFalse : AndTheQueryStringIsWellFormed
{
[OneTimeSetUp]
public new void BeforeAll()
{
base.BeforeAll();
MockSprocketByIdQuery.Reset();
MockSprocketByIdQuery
.Setup(validator => validator.OpenFaultSprocketExists(It.IsAny<int>()))
.Returns(false);
Context.GetWidgetRequest = new GetWidgetRequest
{
Identifier = $"uRn:Crumbly|{ExpectedCrumblySprocket.SprocketId}"
};
ActualResponse = GetWidgetRequestHandler.Get(Context);
}
[Test]
public void ThenTheResponseIsNull() =>
Assert.That(ActualResponse, Is.Null);
}
[TestFixture]
public class AndOpenFaultSprocketExistsReturnsTrue : AndTheQueryStringIsWellFormed
{
[TestFixture]
public class AndValidationResultIsNotFound : AndOpenFaultSprocketExistsReturnsTrue
{
[OneTimeSetUp]
public new void BeforeAll()
{
base.BeforeAll();
MockSprocketValidator.Reset();
MockSprocketValidator
.Setup(validator => validator.ValidateOpenFaultSprocket(It.IsAny<Guid>(), It.IsAny<Guid>()))
.Returns(ValidationResult.NotFound);
Context.GetWidgetRequest = new GetWidgetRequest
{
Identifier = $"uRn:Crumbly|{ExpectedCrumblySprocket.SprocketId}"
};
ActualResponse = GetWidgetRequestHandler.Get(Context);
}
[Test]
public void ThenTheResponseIsNull() =>
Assert.That(ActualResponse, Is.Null);
}
[TestFixture]
public class AndValidationResultIsAccessForbidden : AndOpenFaultSprocketExistsReturnsTrue
{
[OneTimeSetUp]
public new void BeforeAll()
{
base.BeforeAll();
MockSprocketValidator.Reset();
MockSprocketValidator
.Setup(validator => validator.ValidateOpenFaultSprocket(It.IsAny<Guid>(), It.IsAny<Guid>()))
.Returns(ValidationResult.AccessForbidden);
Context.GetWidgetRequest = new GetWidgetRequest
{
Identifier = $"uRn:Crumbly|{ExpectedCrumblySprocket.SprocketId}"
};
ActualResponse = GetWidgetRequestHandler.Get(Context);
}
[Test]
public void ThenTheResponseIsNull() =>
Assert.That(ActualResponse, Is.Null);
}
[TestFixture]
public class AndValidationResultIsSuccess : AndOpenFaultSprocketExistsReturnsTrue
{
[OneTimeSetUp]
public new void BeforeAll()
{
base.BeforeAll();
MockSprocketValidator.Reset();
MockSprocketValidator
.Setup(validator => validator.ValidateOpenFaultSprocket(It.IsAny<Guid>(), It.IsAny<Guid>()))
.Returns(ValidationResult.Success);
Context.GetWidgetRequest = new GetWidgetRequest
{
Identifier = $"uRn:Crumbly|{ExpectedCrumblySprocket.SprocketId}"
};
ActualResponse = GetWidgetRequestHandler.Get(Context);
}
[Test]
public void ThenGetOpenFaultSprocketIsCalled() =>
MockSprocketByIdQuery
.Verify(query => query.GetOpenFaultSprocket(ExpectedCrumblySprocket.SprocketId),
Times.Once);
[Test]
public void ThenValidateOpenFaultSprocketIsCalled() =>
MockSprocketValidator
.Verify(validator => validator.ValidateOpenFaultSprocket(SessionId, ExpectedCrumblySprocket.Id),
Times.Once);
[Test]
public void ThenFactoryBuildIsCalled() =>
MockWidgetFactory
.Verify(factory => factory.Build(ExpectedCrumblySprocket),
Times.Once);
[Test]
public void ThenSerialiseAcmeMessageIsCalled() =>
MockAcmeSerialiser
.Verify(serialiser => serialiser.SerialiseAcmeMessage(It.IsAny<string>(), It.IsAny<Widget>()),
Times.Once);
[Test]
public void ThenTheResponseIsCorrect() =>
Assert.That(ExpectedResponse, Is.EqualTo(ActualResponse));
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.