Created
March 21, 2018 11:05
-
-
Save iamkoch/81cecac9a3c5519a36801e01c8ab4968 to your computer and use it in GitHub Desktop.
Use your autodata attribute like the XBehave Example attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExampleAutoDataSpecimenBuilder : ISpecimenBuilder | |
{ | |
private readonly object[] _data; | |
public ExampleAutoDataSpecimenBuilder(object[] data) | |
{ | |
_data = data; | |
} | |
public object Create(object request, ISpecimenContext context) | |
{ | |
var parameterInfo = request as ParameterInfo; | |
if (parameterInfo == null) | |
return new NoSpecimen(); | |
if (CurrentPositionGreaterThanProvidedExampleCount(parameterInfo)) | |
return new NoSpecimen(); | |
if (ParameterIsType(parameterInfo)) | |
return _data[parameterInfo.Position]; | |
if (ParameterTypesMatch(parameterInfo)) | |
return _data[parameterInfo.Position]; | |
return new NoSpecimen(); | |
} | |
private bool CurrentPositionGreaterThanProvidedExampleCount(ParameterInfo parameterInfo) | |
{ | |
return parameterInfo.Position >= _data.Length; | |
} | |
private bool ParameterTypesMatch(ParameterInfo parameterInfo) | |
{ | |
return _data[parameterInfo.Position] != null && _data[parameterInfo.Position].GetType() == parameterInfo.ParameterType; | |
} | |
private bool ParameterIsType(ParameterInfo parameterInfo) | |
{ | |
return _data[parameterInfo.Position] is Type && parameterInfo.ParameterType == typeof(Type); | |
} | |
} |
Extend the autodata attribute, and add this as a customisation when instantiating the fixture
@iamkoch this seems to need some constructor params, where do those come from when I try to add them to the customizations?
@iamkoch this seems to need some constructor params, where do those come from when I try to add them to the customizations?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@iamkoch How do you configure this to work with autofixture and xbehave?