SpecFlowOSS / SpecFlow

#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration
https://www.specflow.org/
Other
2.25k stars 754 forks source link

StepArgumentTransformations with regex is not working #2676

Closed andyjbenham closed 1 year ago

andyjbenham commented 1 year ago

SpecFlow Version

3.9.74

Which test runner are you using?

NUnit

Test Runner Version Number

3.9.74

.NET Implementation

.NET 6.0

Project Format of the SpecFlow project

Classic project format using <PackageReference> tags

.feature.cs files are generated using

SpecFlow.Tools.MsBuild.Generation NuGet package

Test Execution Method

Visual Studio Test Explorer

SpecFlow Section in app.config or content of specflow.json

No response

Issue Description

Simple transformations for a given type are working but when specifying regex step text - even using a simple example from your documentation the StepArgumentTransformation does not execute and a System.FormatException is generated

Steps to Reproduce

Feature:

Scenario: Step argument test
    Given in 50 days

Step:

[Given(@"in (\d+) days")]
    public void ThenInDays(DateTime p0)
    {
        //do something
    }

Transformation:

[StepArgumentTransformation(@"in (\d+) days")]
public DateTime InXDaysTransform(int days)
{
    return DateTime.Today.AddDays(days);
}

Link to Repro Project

No response

clrudolphi commented 1 year ago

It would seem that the documentation page for StepArgumentTranformation can be misleading and does not contain a complete sample. In the code you provide, both the Step definition and the StepArgumentTransformation attributes use the same Regex which confuses the system (the Step binding matches and so it attempts to process the transformation without invoking the custom StepArgumentTransformer).

A simple fix for the example is to make each Regex unique.


        [Given("in (.*)")]
        public void GivenTheDate(DateTime dt)
        {

        }
        [StepArgumentTransformation(@"(\d+) days")]
        public DateTime InXDaysTransform(int days)
        {
            return DateTime.Today.AddDays(days);
        }

This approach works because the beginning of the binding phrase "in ..." indicates which Binding step to use. The regex of "(\d+) days" indicates which StepArgumentTransformer to use. For example, you might also have other transformers that calculate dates by adding weeks or months or years.

TL;DR: the regex which is used in the StepArgumentTransformer cannot be entire pattern used to match/identify the Step.

HTH

andyjbenham commented 1 year ago

Thanks! That makes sense. Cheers for your prompt response

clrudolphi commented 1 year ago

Thanks! That makes sense. Cheers for your prompt response

Glad to help. Please close this issue if you have no further questions about this topic.

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.