从另一个步骤调用步骤抛出 Object reference not set to instance 错误

Calling step from another step throws Object reference not set to instance error

我有这些 SpecFlow 步骤定义:

    [Given(@"I am on the (.*) page")]
    public void GivenIAmOnThePage(string url)
    {
        Given(@"I go to https://daringfireball.net/ page");
    }

    [Given(@"I go to (.*) page")]
    public void GivenIGoToPage(string url)
    {
        // nothing
    }

当我 运行 测试时,该步骤执行得很好,但在测试结束时抛出错误 运行:

------ Discover test started ------
NUnit Adapter 3.10.0.21: Test discovery starting
Assembly contains no NUnit 3.0 tests: C:\git\MyProj.Tests\bin\Debug\MyProj.Tests.dll
NUnit Adapter 3.10.0.21: Test discovery complete
[xUnit.net 00:00:00.3975558]   Discovering: MyProj.Tests
[xUnit.net 00:00:00.8475597]   Discovered:  MyProj.Tests
========== Discover test finished: 11 found (0:00:00.936) ==========
------ Run test started ------
[xUnit.net 00:00:00.3900232]   Starting:    MyProj.Tests
[xUnit.net 00:00:27.4911173]     View the Daring Fireball archive [FAIL]
[xUnit.net 00:00:27.4958191]       System.NullReferenceException : Object reference not set to an instance of an object.
[xUnit.net 00:00:27.4987971]       Stack Trace:
[xUnit.net 00:00:27.4999882]         C:\git\MyProj.Tests\StepDefinitions\Common\HtmlNavigationSteps.cs(24,0): at MyProj.Tests.StepDefinitions.Common.HtmlNavigationSteps.IGoTo(String text)
[xUnit.net 00:00:27.5010869]            at lambda_method(Closure , IContextManager , String )
[xUnit.net 00:00:27.5022270]            at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
[xUnit.net 00:00:27.5033948]            at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments)
[xUnit.net 00:00:27.5039611]            at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance)
[xUnit.net 00:00:27.5044492]            at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
[xUnit.net 00:00:27.5048664]            at TechTalk.SpecFlow.TestRunner.CollectScenarioErrors()
[xUnit.net 00:00:27.5053708]            at SpecFlow.xUnitAdapter.SpecFlowPlugin.Runners.ScenarioTestCaseRunner.ScenarioCleanup()
[xUnit.net 00:00:27.5060798]            at SpecFlow.xUnitAdapter.SpecFlowPlugin.Runners.ScenarioTestCaseRunner.RunScenario(SpecFlowDocument gherkinDocument, Scenario scenario)
[xUnit.net 00:00:27.5068639]            at SpecFlow.xUnitAdapter.SpecFlowPlugin.Runners.ScenarioTestCaseRunner.<>c__DisplayClass9_0.<RunTestAsync>b__2()
[xUnit.net 00:00:27.5082348]       Output:
[xUnit.net 00:00:27.5090068]         Given I am on the https://daringfireball.net/ page
[xUnit.net 00:00:27.5097676]         -> done: PageNavigation.GivenIAmOnThePage("https://daringfir...") (3.5s)
[xUnit.net 00:00:27.5105438]         When I click the link with text Archive
[xUnit.net 00:00:27.5113023]         When I go to https://dev-apdirect.airproducts.com/AgentsPlus/CustomerSearch.aspx
[xUnit.net 00:00:27.5121925]         -> error: Object reference not set to an instance of an object.
[xUnit.net 00:00:27.5129174]         -> done: HtmlNavigationSteps.WhenIClickTheLinkWithText("Archive") (0.0s)
[xUnit.net 00:00:27.5135176]         Then I will arrive on the page titled Daring Fireball: Archive
[xUnit.net 00:00:27.5142561]         -> skipped because of previous errors
[xUnit.net 00:00:27.5296309]   Finished:    MyProj.Tests
========== Run test finished: 1 run (0:00:27.615) ==========

当我直接调用 GivenIGoTo() 方法而不使用 Given() 时,我得到:

------ Discover test started ------
NUnit Adapter 3.10.0.21: Test discovery starting
Assembly contains no NUnit 3.0 tests: C:\git\MyProj.Tests\bin\Debug\MyProj.Tests.dll
NUnit Adapter 3.10.0.21: Test discovery complete
[xUnit.net 00:00:00.4054618]   Discovering: MyProj.Tests
[xUnit.net 00:00:00.8653138]   Discovered:  MyProj.Tests
========== Discover test finished: 11 found (0:00:00.949) ==========
------ Run test started ------
[xUnit.net 00:00:00.3959460]   Starting:    MyProj.Tests
[xUnit.net 00:00:27.6141096]     View the Daring Fireball archive [FAIL]
[xUnit.net 00:00:27.6166878]       System.NullReferenceException : Object reference not set to an instance of an object.
[xUnit.net 00:00:27.6184162]       Stack Trace:
[xUnit.net 00:00:27.6190119]         C:\git\MyProj.Tests\StepDefinitions\Common\HtmlNavigationSteps.cs(24,0): at MyProj.Tests.StepDefinitions.Common.HtmlNavigationSteps.IGoTo(String text)
[xUnit.net 00:00:27.6196352]         C:\git\MyProj.Tests\StepDefinitions\Common\HtmlNavigationSteps.cs(18,0): at MyProj.Tests.StepDefinitions.Common.HtmlNavigationSteps.WhenIClickTheLinkWithText(String text)
[xUnit.net 00:00:27.6204164]            at lambda_method(Closure , IContextManager , String )
[xUnit.net 00:00:27.6210895]            at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
[xUnit.net 00:00:27.6217649]            at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments)
[xUnit.net 00:00:27.6225196]            at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance)
[xUnit.net 00:00:27.6232286]            at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
[xUnit.net 00:00:27.6238029]            at TechTalk.SpecFlow.TestRunner.CollectScenarioErrors()
[xUnit.net 00:00:27.6244058]            at SpecFlow.xUnitAdapter.SpecFlowPlugin.Runners.ScenarioTestCaseRunner.ScenarioCleanup()
[xUnit.net 00:00:27.6250710]            at SpecFlow.xUnitAdapter.SpecFlowPlugin.Runners.ScenarioTestCaseRunner.RunScenario(SpecFlowDocument gherkinDocument, Scenario scenario)
[xUnit.net 00:00:27.6257423]            at SpecFlow.xUnitAdapter.SpecFlowPlugin.Runners.ScenarioTestCaseRunner.<>c__DisplayClass9_0.<RunTestAsync>b__2()
[xUnit.net 00:00:27.6270580]       Output:
[xUnit.net 00:00:27.6276855]         Given I am on the https://daringfireball.net/ page
[xUnit.net 00:00:27.6282959]         -> done: PageNavigation.GivenIAmOnThePage("https://daringfir...") (3.6s)
[xUnit.net 00:00:27.6289091]         When I click the link with text Archive
[xUnit.net 00:00:27.6295082]         -> error: Object reference not set to an instance of an object.
[xUnit.net 00:00:27.6301183]         Then I will arrive on the page titled Daring Fireball: Archive
[xUnit.net 00:00:27.6308160]         -> skipped because of previous errors
[xUnit.net 00:00:27.6416392]   Finished:    MyProj.Tests
========== Run test finished: 1 run (0:00:27.721) ==========

我在输出窗格的末尾也看到了这个:

System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain. This can happen if the test(s) started a thread but did not stop it. Make sure that all the threads started by the test(s) are stopped before completion.

简短的回答是,这是非常不明智的。我不完全确定为什么 API 据说在 SpecFlow 如此脆弱的情况下支持它。

据称:

不要: