Skip to content

Instantly share code, notes, and snippets.

@markryd
Last active October 7, 2015 03:13
Show Gist options
  • Select an option

  • Save markryd/ebf24e91edbee18e622e to your computer and use it in GitHub Desktop.

Select an option

Save markryd/ebf24e91edbee18e622e to your computer and use it in GitHub Desktop.
//_fixture.Call(patient, "UpdateDemographics");
//call a method with values from autofixture.
public static T Call<T>(this T component, Fixture fixture, string methodName) where T : class
{
var methodInfo = typeof(T).GetMethod(methodName);
var fixtureCreateMethod = typeof(SpecimenFactory)
.GetMethods()
.Where(m => m.Name == "Create" && m.IsGenericMethodDefinition)
.Select(m => new
{
Method = m,
Params = m.GetParameters(),
})
.Where(a => a.Params.Length == 1 && a.Params[0].ParameterType == typeof(ISpecimenBuilder))
.Select(a => a.Method)
.Single();
var stringFixtureCreateMethod = typeof(SpecimenFactory)
.GetMethods()
.Where(m => m.Name == "Create" && m.IsGenericMethodDefinition)
.Select(m => new
{
Method = m,
Params = m.GetParameters(),
})
.Where(a => a.Params.Length == 2 && a.Params[0].ParameterType == typeof(ISpecimenBuilder))
.Select(a => a.Method)
.Single();
var parameters = methodInfo
.GetParameters()
.Select(x => x.ParameterType == typeof(string)
? stringFixtureCreateMethod.MakeGenericMethod(x.ParameterType).Invoke(null, new object[] {fixture, x.Name}) //this one sticks the param name on the start of the created string
: fixtureCreateMethod.MakeGenericMethod(x.ParameterType).Invoke(null, new object[] {fixture}))
.ToArray();
methodInfo.Invoke(component, parameters);
return component;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment