Is the following MS VS solution structure and classes OK w.r.t. easy unit tests with dependency injection? Would your unit tests look similar?
If you call the calculations below over a WPF module, would you skip a test over the WPF since complicated, or would you still conisder testing it over the WPF module ?
Solution
ProjectWork
Classes
IDev.cs
public interface IDev { IToMeasure Measure(); } public interface IToMeasure { double Value{ get; set; } }
CalculatingUnit.cs
public class CalculatingUnit { int count = 0, maxMeasuredValues = 20; public double doMeasures(IDev myDevice) { // simulating measuring each Xms while (count < maxMeasuredValues ) { return myDevice.Measure(); count++; } } }
MyDevice.cs
// non-deterministic (random) values public class MyDevice: IDev { private readonly MyRandom myRandomVar = new Random(0); public IToMeasure Measure() { var meas = new Measure { Value = myRandomVar.NextRandom(2, 0.5) }; return meas; } }
TestDevice.cs
// deterministic values public class TestDevice : IDevice { int nrValues = 6; int gIdx = 0, gCount = 0; int[] testMeasures = { 1, 2, 3, 4, 5, 6 }; public IToMeasure Measure() { gIdx = gCount % nrValues; var meas = new Measure { Value = testMeasures[gIdx] }; gCount++; return meas; } }
myDeviceWPF.cs // it seems complicated testing over WPF, so I dont test with it, would you also skip it ?
class myDeviceViewModel : INotifyPropertyChanged { ... }
ProjectTest
myDevice_Test.cs
[TestClass] public class myDevice_Test { [TestMethod] public void Test_doMeasures() { double actual = 1; double expected = 21.068; TestDevice myDevice; CalculatingUnit myCalculatingUnit; myDevice = new TestDevice(); myCalculatingUnit = new CalculatingUnit(); actual = myCalculatingUnit.doMeasures(myDevice ); Assert.AreEqual(expected, actual , 0.001, "Wrong calculations"); } }
}
sure it was a typo but thought I would point it out \$\endgroup\$