I have an interface IAudioProcessor
with a single method IEnumerable<Sample> Process(IEnumerable<Sample> samples)
. While it is not a requirement of the interface itself, I want to make sure that all my implementations follow some common rules, like for example:
- Use deferred execution
- Don't change the input samples
It is not hard to create tests for these, but I would have to copy and paste these tests for each implementation. I would like to avoid that.
I would like to do something like this (note the attribute GenericTest
and the type parameter):
[GenericTest(typeof(AudioProcessorImpl1Factory))]
[GenericTest(typeof(AudioProcessorImpl2Factory))]
[GenericTest(typeof(AudioProcessorImpl3Factory))]
public class when_processed_audio_is_returned<TSutFactory>
where TSutFactory : ISutFactory<IAudioProcessor>, new()
{
static IAudioProcessor Sut = new TSutFactory().CreateSut();
protected static Context _ = new Context();
Establish context = () => _.Original = Substitute.For<IEnumerable<ISample>>();
Because of = () => Sut.Process(_.Original);
It should_not_have_enumerated_the_original_samples = () =>
{
_.Original.DidNotReceive().GetEnumerator();
((IEnumerable)_.Original).DidNotReceive().GetEnumerator();
};
}
Is something like this possible?