Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 }
like image 805
procma Avatar asked Oct 25 '25 04:10

procma


1 Answers

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);
    }
like image 100
Meir Kriheli Avatar answered Oct 27 '25 01:10

Meir Kriheli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!