Format a string in SpecFlow using Step Argument Conversion

I wanted to know if it is possible to convert a string to another string using the Step Argument Conversion. Here is an example:

I have steps like:

When in element 'Element' I enter 'value'

Those steps have to accept different data, like a number, a date and so on. Their definition are like this:

    [When(@"in element '(.*)' I enter '(.*)'")]
    public void WhenIEnterInElement(string element, string value)
    {
        Enter(value, element);
    }

I would like it to accept things like:

When in element "Element" I enter "today plus 3 days"

And use a Step Argument Conversion like:

    [StepArgumentTransformation(@"today plus (\d+) days")]
    public string ConvertDate(int days)
    {
        return DateTime.Today.AddDays(days).ToString();
    }

It isn't working because I'm trying to convert a string to a srting am I right? Isn't there a way to do this with Step Argument Conversion?

Answers


It sounds like what you really want is to reformat a string, so that you can restrict yourself to a single binding method.

But

The following conversions can be performed by SpecFlow (in the following precedence):
  • no conversion, if the argument is an instance of the parameter type (e.g. the parameter type is object or string)
  • step argument transformation
  • standard conversion
  • (From https://github.com/techtalk/SpecFlow/wiki/Step-Argument-Conversions)

    Since your method takes a string parameter, no stepargumenttransformations will be used.

    You have 2 options

    1) use transformations for each data type and a method binding for each type

    [When(@"in element '(.*)' I enter '(.*)'")]
    public void WhenIEnterInElement(string element, DateTime value)
    {
        Enter(value, element.ToString());
    }
    
    [StepArgumentTransformation(@"today plus (\d+) days")]
    public DateTime ConvertDate(int days)
    {
        return DateTime.Today.AddDays(days);
    }
    

    2) Wrap string for the purposes of being able to reuse your step bindings with different unparsed data

    public class WrappedString
    {
        public string Value;
        public WrappedString(string value):Value(value) {}
    }
    
    [When(@"in element '(.*)' I enter '(.*)'")]
    public void WhenIEnterInElement(string element, WrappedString value)
    {
        Enter(value, element.Value);
    }
    
    [StepArgumentTransformation(@"today plus (\d+) days")]
    public WrappedString ConvertDate(int days)
    {
        return new WrappedString(DateTime.Today.AddDays(days).ToString());
    }
    

    1) is perhaps cleaner, but it sounds like your scenario is based a little around being able to manipulate text - in which case (2) may even help model that requirement.


    Need Your Help

    Which is better in C allocating/deallocating memory of using a local variable? and Why?

    c pointers allocation memory-management

    If I need to do string manipulation or manipulating any kind of arrays be it stantard types like int or a self defined data structure. What is better local variable or dynamically allocating and

    UIViewController hidesBottomBarWhenPushed puts the UIView in the wrong place during push/pop animation

    ios uinavigationcontroller uikit core-animation uitoolbar

    I have two UIViewControllers within a UINavigationController. The topmost has hidesBottomBarWhenPushed set to YES. When I push the topmost UIViewController, its UITableView is pushed about 20px above

    About UNIX Resources Network

    Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.