2

Is there any way to use Strongly-Typed values in Examples table of the scenario? (or alternative solution)

I'd like to know if I made a typo in userType column already during the coding (not during running the test).

UPDATED

file.feature

Scenario Outline: Scenario123
Given Create new user of type "<userType>"
Examples:
| userType     |
| PlatinumUser |
| CommonUser   |

steps.cs

[Given(@"Create new user of type ""(.*)""")]
public void CreateNewUser(UserTypeEnum userType)
{
    // some code like e.g.:
    MyUser user = new MyUser(userType);
    //...
}

enum UserTypeEnum  { CommonUser, PlatinumUser, Spectre }
procma
  • 1,174
  • 3
  • 14
  • 24
  • This example looks a little too vague. I'm not clear about how you expect to map the table to strongly typed values. – Greg Burghardt Apr 03 '19 at 21:50
  • Can you post more of the steps in your outline? How are each of these values used? Their types will be determined at test execution time by the step in which they are used. – Greg Burghardt Apr 03 '19 at 21:51

2 Answers2

2

Looks like its a StepArgumentTransformation that you are after?

https://github.com/techtalk/SpecFlow/wiki/Step-Argument-Conversions

Used somehow along these lines:

[Binding]
public class Transforms
{
    [StepArgumentTransformation]
    public UserTypeEnum UserTypeTransform(string UserType)
    {
      // return the string converted into the required Enum
    }
}

The step binding will see that it requires a UserTypeEnum as a parameter so it will search for all the available Step Argument Transformations within any classes with the Binding attribute and use this method to perform the conversion.

D. Hawes
  • 46
  • 2
2

Specflow supports accepting strongly typed enum values. Though, the scenario sends it as text (case insensitive).

example:

    Scenario: Some enum test  
        When I send enum "Second"        
        Then I get the second enum
    public enum ChosenOption
    {
        First,
        Second,
        Third,
    }
    
    [When(@"I send enum ""(.*)""")]
    public void WhenISendEnum(ChosenOption option)
    {
        _scenarioContext.Set(option, nameof(ChosenOption));
    }

    [Then(@"I get the second enum")]
    public void ThenIGetTheSecondEnum()
    {
        var chosen = _scenarioContext.Get<ChosenOption>(nameof(ChosenOption));
        chosen.Should().Be(ChosenOption.Second);
    }