Skip to content

Instantly share code, notes, and snippets.

@westonal
Created June 26, 2015 09:29
Show Gist options
  • Save westonal/8e30aad8b6e01edb98bb to your computer and use it in GitHub Desktop.
Save westonal/8e30aad8b6e01edb98bb to your computer and use it in GitHub Desktop.
Specs from fields
public readonly object Spec = "Public spec field"
.Assert(() => Assert.Pass("It works!"));
private object Spec2 = "Private spec field"
.Assert(() => Assert.Pass("It works!"));
private object Spec3 = new[]
{
"Collection spec field"
.Assert(() => Assert.Pass("It works!")),
"Collection spec field"
.Assert(() => Assert.Pass("It works!"))
};
public abstract class SpecificationFromFields : Specification
{
protected override ISpecification[] TestCases()
{
var values = GetType()
.GetFields(BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance)
.Select(f => f.GetValue(this)).ToList();
var specs = values.OfType<ISpecification>();
var enumSpecs = values.OfType<ICollection<ISpecification>>().SelectMany(e => e);
return specs.Concat(enumSpecs).ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment