2

How would on go about unit testing a method like this? Is it even possible when it's a void?

public static void CalcDeltaCO2 ()
    {
        double plantCO2usage = CalcPlantCO2Usage ();
        double airShiftsPerHour = 1 + instance._greenhouse.WindowRoof.GetStatus ();
        double co2AtStart = _simulatedChunks > 1 ? Toolbox.PPMtoM3perM3 (instance._currentCO2Sensor) : Toolbox.CO2_LEVEL_OUTSIDE;
        instance._deltaCO2 = Toolbox.M3perM3toPPM ((((instance._greenhouse.CO2Dispenser.GetFlow ()) - plantCO2usage
        / (airShiftsPerHour * instance._greenhouse.Volume))
        * (1.0 - (1.0 / Math.Pow (Math.E, (airShiftsPerHour * Toolbox.CHUNK_TIME_H)))) + (Toolbox.CO2_LEVEL_OUTSIDE - co2AtStart)
        * (1.0 / Math.Pow (Math.E, (airShiftsPerHour * Toolbox.CHUNK_TIME_H))) + co2AtStart)
        - Toolbox.PPMtoM3perM3 (instance._currentCO2Sensor));
    }
  • 2
    A `void` method will typically affect an application's state (aka: a variable gets changed somewhere). In your case you seem to be changing `instance._deltaCO2` so mock/stub that out and verify changes against it. – Jeroen Vannevel May 18 '14 at 15:01

1 Answers1

2

Unit tests can verify one of 3 things:

  1. method returned a value
  2. method changed state
  3. method threw an exception

In your case, your method changes the state by doing some calculations. What you could do in this case, is to verify that instance._deltaCO2 is of the expected value.

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
  • +1 for this specific test. Other things unit tests can verify are that behaviours (of the object or of the application) are correctly affected by having made the call. NUnit can also verify that calling the method a) specifically does not throw any exception and b) returns within a specified time. – ClickRick May 18 '14 at 15:05