.NET 4.7.2单元测试IEnumerable在Azure DevOps流水线中异常
问题背景
我有一个基于xUnit、.NET 4.7.2的单元测试,用于测试自定义的IEnumerable处理函数,断言语句如下:
adt.PatientVisitInformation.First().AssignedPatientLocation.Facility.Value.ShouldBe("Arundel");
该测试在本地Visual Studio中运行正常,但在Azure DevOps流水线(使用ubuntu-latest虚拟机镜像)执行时抛出异常。
错误信息
Error Message:
System.InvalidOperationException : Sequence contains no elements
Stack Trace:
at System.Linq.Enumerable.First[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00010] in <69ada62907b24213a012734531df1db1>:0
at My.Products.HL7.Parser.UnitTests.Definitions.Messages.ADT_ATests.Decode_ADTAMessage (System.String ADT) [0x00321] in <36abcadd6a6a4eadb1f459337a2a48d7>:0
at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in:0
Failed My.Products.HL7.Parser.UnitTests.Definitions.Messages.ADT_ATests.Decode_ADTAMessage(ADT: "ADT^A01") [7 ms]
流水线中IEnumerable返回内容
System.Linq.Enumerable+<OfTypeIterator>d__32`1[My.Products.HL7.Parser.Definitions.Segments.PV1]
Azure DevOps流水线任务配置
- task: UseDotNet@2 inputs: packageType: 'sdk' version: '8.x' # Specify the .NET SDK version installationPath: $(Agent.ToolsDirectory)/dotnet - task: UseDotNet@2 inputs: packageType: 'sdk' version: '6.0.x' # Optional: If you need a .NET 6 SDK for any reason installationPath: $(Agent.ToolsDirectory)/dotnet - script: dotnet nuget locals all --clear displayName: 'Clear NuGet caches' - task: DotNetCoreCLI@2 displayName: 'Restore NuGet Packages' inputs: command: 'restore' projects: '**Integrations/**/*.csproj' - task: DotNetCoreCLI@2 displayName: 'Build Function - Custom Function' inputs: command: 'build' projects: '**Integrations/**/*.csproj' arguments: '/p:LogicAppFolder=Test-Interface-LogicApp\LogicApp-${{parameters.testInterface}}' - task: DotNetCoreCLI@2 displayName: 'Test Custom Functions' inputs: command: test projects: | **Integrations/**/*.UnitTests.csproj arguments: '--logger "console;verbosity=detailed"' publishTestResults: true
使用的虚拟机镜像为ubuntu-latest
内容的提问来源于stack exchange,提问作者Joy Alecha

