Created
December 21, 2015 08:05
-
-
Save gasparnagy/a478e5b7ccb8f557a6dc to your computer and use it in GitHub Desktop.
Configure SpecFlow v2 Assist Table helpers to use [StepArgumentTransformation] extensions
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
/// <summary> | |
/// Sets the step argument conversion infrasuructure as default for CreateSet and CreateInstance | |
/// </summary> | |
/// <remarks> | |
/// This method has to be called once, in a static ctor for example. Note: this way of setting is not | |
/// supported for parallel execution. | |
/// </remarks> | |
private static void SetupStepArgumentConverterValueRetriever() | |
{ | |
var assistService = TechTalk.SpecFlow.Assist.Service.Instance; | |
// unregistering all existing retrievers | |
foreach (var valueRetriever in assistService.ValueRetrievers.ToArray()) | |
assistService.UnregisterValueRetriever(valueRetriever); | |
// register retriever that uses the SpecFlow step argument conversion mechanizm | |
assistService.RegisterValueRetriever(new StepArgumentConverterValueRetriever()); | |
} | |
/// <summary> | |
/// Sets the step argument conversion infrasuructure as default for CompareToSet and CompareToInstance | |
/// </summary> | |
/// <remarks> | |
/// This method has to be called once, in a static ctor for example. Note: this way of setting is not | |
/// supported for parallel execution. | |
/// </remarks> | |
private static void SetupStepArgumentConverterValueComparer() | |
{ | |
var assistService = TechTalk.SpecFlow.Assist.Service.Instance; | |
// unregistering all existing retrievers | |
foreach (var valueComparer in assistService.ValueComparers.ToArray()) | |
assistService.UnregisterValueComparer(valueComparer); | |
// register comparer that uses the SpecFlow step argument conversion mechanizm | |
assistService.RegisterValueComparer(new StepArgumentConverterValueComparer()); | |
} | |
internal class StepArgumentConverterValueRetriever : IValueRetriever | |
{ | |
public bool CanRetrieve(KeyValuePair<string, string> keyValuePair, Type targetType, Type propertyType) | |
{ | |
var converter = ScenarioContext.Current.ScenarioContainer.Resolve<IStepArgumentTypeConverter>(); | |
return converter.CanConvert(keyValuePair.Value, new RuntimeBindingType(propertyType), CultureInfo.CurrentCulture); | |
} | |
public object Retrieve(KeyValuePair<string, string> keyValuePair, Type targetType, Type propertyType) | |
{ | |
var converter = ScenarioContext.Current.ScenarioContainer.Resolve<IStepArgumentTypeConverter>(); | |
return converter.Convert(keyValuePair.Value, new RuntimeBindingType(propertyType), CultureInfo.CurrentCulture); | |
} | |
} | |
internal class StepArgumentConverterValueComparer : IValueComparer | |
{ | |
public bool CanCompare(object actualValue) | |
{ | |
return true; | |
} | |
public bool Compare(string expectedValue, object actualValue) | |
{ | |
var converter = ScenarioContext.Current.ScenarioContainer.Resolve<IStepArgumentTypeConverter>(); | |
var convertedExpectedValue = actualValue == null ? expectedValue : | |
converter.Convert(expectedValue, new RuntimeBindingType(actualValue.GetType()), CultureInfo.CurrentCulture); | |
return Equals(actualValue, convertedExpectedValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!
Edit: no idea why this isn't the default.