我想在 .NET 7 MAUI 应用程序上对视图模型、服务等运行单元测试。我关注 this,但它是针对 .NET 6 的,所以我发现 this 是针对 .NET 7 的,但似乎都使用相同的流程。无论如何,我在将
net7.0
添加到目标框架后按照步骤操作,所有 nuget 包都被删除,并且在尝试构建时出现以下错误:
error NETSDK1005: Assets file 'C:...project\obj\project.assets.json' doesn't have a target for 'net7.0'. Ensure that restore has run and that you have included 'net7.0' in the TargetFrameworks for your project.
error NU1201: Project is not compatible with net7.0 (.NETCoreApp,Version=v7.0). Project does not support any target frameworks.
主要项目:
<TargetFrameworks>net7.0;net7.0-android33.0</TargetFrameworks> <OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType>
xUnit项目:
<TargetFramework>net7.0</TargetFramework>
建议卸载/重新加载我的项目并重新启动 Visual Studio,我尝试过但没有任何效果。我该如何解决这个问题?
Gerald Versluis 在他的视频中找到并提出了解决方案
使用 xUnit 对 .NET MAUI 应用程序进行单元测试
总结:
之前
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
之后
<TargetFrameworks>net8.0;net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
之前
<OutputType>Exe</OutputType>
之后
<OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>
第一个答案效果很好!只是想添加旁注。不要更改 ApplicationDisplayVersion,它必须是 1 或 1.0,否则解决方案不起作用。由于 Visual Studio 中的bug。