Last active
October 7, 2015 03:13
-
-
Save markryd/ebf24e91edbee18e622e to your computer and use it in GitHub Desktop.
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
| //_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