Search code examples
specflowfeature-file

Strongly-Typed values in specflow scenario


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 }

Solution

  • 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);
        }