首页 > 解决方案 > 获取[当前执行的线程不在步骤定义上下文中]错误

问题描述

我的 ASP.NET Core 3 API 的 BDD 样式单元测试出错。引用的包 (xunit:2.4.1, xbehave:2.4.1,xunit.runner.visualstudio:2.4.2)。

这是导致相同错误的简化示例测试:

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

并运行测试抛出异常:

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

标签: c#unit-testingxunit.netxbehave.net

解决方案


适用于旧版本:(xunit:2.4.0, xbehave:2.4.0)

但不适用于最新版本:(xunit:2.4.1, xbehave:2.4.1)

版本:xunit.runner.visualstudio 无关紧要。它适用于(2.4.0 或 2.4.2)。

希望这可以帮助某人。


推荐阅读