I do a lot of unit testing(tdd) and would like some comments about my style. Here is a simple example from a composite.
Is there anything I could improve?
public class ChangedClaimsServiceComposite : IChangedClaimsService
{
private readonly IChangedClaimsService[] changedClaimsServices;
public ChangedClaimsServiceComposite(params IChangedClaimsService[] changedClaimsServices)
{
if (changedClaimsServices == null) throw new ArgumentNullException("changedClaimsServices");
this.changedClaimsServices = changedClaimsServices;
}
public List<IChangedClaim> GetChangedClaims()
{
return changedClaimsServices.SelectMany(x => x.GetChangedClaims()).ToList();
}
public void SetClaimUpdated(IChangedClaim changedClaim)
{
foreach (var changedClaimsService in changedClaimsServices)
{
changedClaimsService.SetClaimUpdated(changedClaim);
}
}
}
[TestFixture]
public class ChangedClaimsServiceCompositeTests
{
[Test]
public void GetChangedClaimsSouldCombineResultsFromDependedServices()
{
//Arrange
var changedClaimService1 = new Mock<IChangedClaimsService>();
var changedClaimService2 = new Mock<IChangedClaimsService>();
var changedClaim1 = CreateAnyChangedClaim();
var changedClaim2 = CreateAnyChangedClaim();
var changedClaim3 = CreateAnyChangedClaim();
var changedClaim4 = CreateAnyChangedClaim();
changedClaimService1.Setup(x => x.GetChangedClaims())
.Returns(new List<IChangedClaim>() {changedClaim1, changedClaim2});
changedClaimService2.Setup(x => x.GetChangedClaims())
.Returns(new List<IChangedClaim>() { changedClaim3, changedClaim4 });
var sut = new ChangedClaimsServiceComposite(
changedClaimService1.Object,
changedClaimService2.Object);
//Act
var result = sut.GetChangedClaims();
//Assert
var expectedResult = new List<IChangedClaim>()
{
changedClaim1,
changedClaim2,
changedClaim3,
changedClaim4
};
Assert.That(result, Is.EqualTo(expectedResult), "result");
}
[Test]
public void SetClaimUpdatedShouldCallSameMethodOnDependedServices()
{
//Arrange
var changedClaimService1 = new Mock<IChangedClaimsService>();
var changedClaimService2 = new Mock<IChangedClaimsService>();
var changedClaim = CreateAnyChangedClaim();
var sut = new ChangedClaimsServiceComposite(
changedClaimService1.Object,
changedClaimService2.Object);
//Act
sut.SetClaimUpdated(changedClaim);
//Assert
changedClaimService1.Verify(x => x.SetClaimUpdated(changedClaim), "changedClaimService1");
changedClaimService2.Verify(x => x.SetClaimUpdated(changedClaim), "changedClaimService2");
}
private IChangedClaim CreateAnyChangedClaim()
{
return new Mock<IChangedClaim>().Object;
}
}