How to know the expected result in Test Driven Development when using Constrained Non-Determinism without duplicating Logic
I am currently reading the (not yet completed) ebook "Test Driven Developlment - Extensive Tutorial" by Grzegorz Gałęzowski. There is a chapter featuring Mark Seeman's idea of constrained non-determinism which basically means that one should use anonymous inputs independently generated within a defined range as input for testing. As an example, you can think of a class like public class Division { private double a, b; public Division(double a, double b) { this.a = a; this.b = b; } public double Perform() => a/b; } to be tested. The test would look like this: [Fact] public void ShouldReturnQuotientIfDenominatorNotZero() { // Given double numerator = Any.Double(); double denominator = Any.DoubleExcept(0); var division = new Division(numerator, denominator); // When var result = division.Perform(); // Then Assert.AreEqual(numerator/denominator, result); } My problem in this very simple example is that the numerator/denominator in the Assert statement is already a duplication of the a/b in the Division's Perform method. How would one do this in a real world scenario? Having the same logic in the test and in the implementation obviously does not make sense.
I am currently reading the (not yet completed) ebook "Test Driven Developlment - Extensive Tutorial" by Grzegorz Gałęzowski. There is a chapter featuring Mark Seeman's idea of constrained non-determinism which basically means that one should use anonymous inputs independently generated within a defined range as input for testing.
As an example, you can think of a class like
public class Division
{
private double a, b;
public Division(double a, double b)
{
this.a = a;
this.b = b;
}
public double Perform() => a/b;
}
to be tested. The test would look like this:
[Fact]
public void ShouldReturnQuotientIfDenominatorNotZero()
{
// Given
double numerator = Any.Double();
double denominator = Any.DoubleExcept(0);
var division = new Division(numerator, denominator);
// When
var result = division.Perform();
// Then
Assert.AreEqual(numerator/denominator, result);
}
My problem in this very simple example is that the numerator/denominator
in the Assert statement is already a duplication of the a/b
in the Division's Perform method.
How would one do this in a real world scenario? Having the same logic in the test and in the implementation obviously does not make sense.