Sunday, June 3, 2012

Testing the Sad Path

When_passing_null_measurements.cs

using NUnit.Framework;

namespace Domain.Tests.AveragingCalculator_Tests.SadPath
{
[Category("AveragingCalculator")]
public class When_passing_null_measurements
{

private AveragingCalculator _averageCalculator;

[SetUp]
public void Setup()
{
_averageCalculator = new AveragingCalculator();
}

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void When_measurement_is_null()
{
var measurements = Mother.Get4Measurements();
measurements.Add(null);

_averageCalculator.Aggregate(measurements);
}
}

AveragingCalculator.cs

namespace Domain
{
public class AveragingCalculator : IAggregateCalculator
{
if(measurements.Contains(null))
{
throw new ArgumentNullException();
}

return new Measurement()
{
HighValue=measurements.Average(m=>m.HighValue),
LowValue=measurements.Average(m=>m.LowValue)
};

}

Test Project Structure

Solution

  • Domain
    • AggregationType.cs
    • AveragingCalculator.cs
    • IAggregateCalculator.cs
    • IGrouper.cs
    • Measurement.cs
    • MeasurementAggregator.cs
    • ModalCalculator.cs
    • SizeGrouper.cs
  • Domain.Tests
    • References
      • Domain
    • AveragingCalculator_Tests
      • SadPath
        • When_passing_null_measurements.cs
          • When_measurements_are_null()
          • When_measurent_is_null()
      • When_averaging_4_numbers.cs
      • When_averaging_several_numbers.cs
    • MeasurementAggregator_Tests
      • When_aggregating_four_measurements.cs
    • Mother.cs