Getting [The currently executing thread is not in a step definition context] error

270 views Asked by At

BDD style unit test for my ASP.NET Core 3 API is erroring. Packages referenced (xunit:2.4.1, xbehave:2.4.1,xunit.runner.visualstudio:2.4.2).

Here is a simplified sample test that results in the same error:

[Fact]
public void Test1()
{
    int x=0, 
        y=0, 
        sum = 0;

    "Given x = 20".x(() => x = 20);
    "And y = 10".x(() => y = 10);
    "When adding x & y".x(() => sum = x + y);
    "Then 30 is returned".x(() => Assert.Equal(30, sum));
}

And running the test throw exception:

Message: System.InvalidOperationException : The currently executing thread is not in a step definition context. Stack Trace: CurrentThread.Add(IStepDefinition item) StringExtensions.x(String text, Action body) UnitTest1.Test1() line 15

2

There are 2 answers

0
mai On

Use [Scenario] attribute instead of [Fact]:

[Scenario]
public void Test1()
{
   //...
}
0
Koref Koref On

Works with older versions: (xunit:2.4.0, xbehave:2.4.0)

But does NOT work with latest versions: (xunit:2.4.1, xbehave:2.4.1)

Version of: xunit.runner.visualstudio is insignificant. It works either with (2.4.0, or 2.4.2).

Hope this helps someone.