0

In Specflow 3.1 NullValueRetriever can be used to specify the character sequence to identify which table values should be translated to NULL (like {NULL}). In Specflow 3.3 ValueRetriever's have been refactored and NullValueRetriever was removed.

I would like to know how should I replace NullValueRetriever functionality, and indicate that I want to have a NULL value for the specific property of the object.

Code for the ClassRetriever, or StringRetriever indicate that empty cells in Tables will be converted to String.Empty instead of null.

GKalnytskyi
  • 579
  • 7
  • 16

2 Answers2

1

That was an error, that we removed the NullValueRetriever. Sorry.
It is the only retriever, that is not by default in the list, as it has to be configured by the user.

I added it back again in PR https://github.com/SpecFlowOSS/SpecFlow/pull/2067. It should be back in a release after 3.3.57.

Andreas Willich
  • 5,665
  • 3
  • 15
  • 22
0

Here is how I implemented Null value retriever for SpecFlow 3.3

public class NullValueRetriever : IValueRetriever
{
    private readonly string _NullPattern;

    public NullValueRetriever(string pattern)
    {
        if (string.IsNullOrWhiteSpace(pattern))
        {
            throw new ArgumentException("Null, or empty strings are not allowed", nameof(pattern));
        }
        _NullPattern = pattern;
    }

    public bool CanRetrieve(KeyValuePair<string, string> keyValuePair, Type targetType, Type propertyType)
    {
        var value = keyValuePair.Value;

        // Refer to: https://stackoverflow.com/a/46450891/761755
        bool isNullableType = !propertyType.IsValueType ||
            (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>));

        return isNullableType &&
            value != null &&
            (string.Compare(value.Trim(), _NullPattern, StringComparison.InvariantCultureIgnoreCase) == 0);
    }

    public object Retrieve(KeyValuePair<string, string> keyValuePair, Type targetType, Type propertyType)
    {
        return null;
    }
}
GKalnytskyi
  • 579
  • 7
  • 16