I have a method that takes multiple implementations of a single-method interface and executes them in turn. Each of these mutates ValueType
in some way.
public interface IValueTypeMutator
{
ValueType ModifyValueType(ValueType valueType);
}
public class ValueTypeBuilder
{
public ValueType Create(params IValueTypeMutator[] mutators)
{
var valueType = new ValueType { X = "SomeConstant" };
return mutators.Aggregate(
(valueType, mutator) => mutator.ModifyValueType(valueType));
}
}
When I'm testing a class that uses a ValueTypeBuilder
, each IValueTypeMutator
will be a stub. To maintain the state of my ValueType
after initialisation, for some tests, I want the stub to simply echo the value of the ValueType
input as the return value.
This is necessary because without an explicit Stub()
implementation, the default behaviour of the stub is to return null
, which will overwrite the initialisation of ValueType
earlier in the Create()
method. Defining the actual values used in ValueType
's initialisation would, however, cause the tests to become unnecessarily brittle: the precise initialisation of ValueType
is not relevant to these particular tests, just that its state is maintained.