如何在Central Package Management中固定FluentAssertions 7.1.0版本?
解决方案:固定FluentAssertions版本并禁用更新提示
1. 修正Central Package Management的版本配置问题
你当前Directory.Packages.props中的条件判断可能无法正确识别测试项目(因该文件的属性评估时机与项目文件存在差异),可通过以下两种方式调整:
方式一:全局统一固定版本
若所有测试项目都需要使用7.1.0版本,直接移除条件判断,简化配置:
<Project> <ItemGroup> <PackageVersion Include="FluentAssertions" Version="7.1.0" /> </ItemGroup> </Project>
方式二:精准匹配测试项目
如需仅对测试项目生效,先修改Directory.Build.props,确保IsTestProject属性能被全局读取:
<Project> <PropertyGroup> <TargetFramework>net9.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> </PropertyGroup> <PropertyGroup Condition="$(MSBuildProjectName.EndsWith('.Tests'))"> <IsTestProject>true</IsTestProject> <ExportProperties>$(ExportProperties);IsTestProject</ExportProperties> </PropertyGroup> </Project>
之后保留Directory.Packages.props的原有条件配置即可。
2. 禁用NuGet版本更新提示
方法一:通过包引用配置禁用警告
在Directory.Packages.props中为FluentAssertions添加版本警告屏蔽:
<Project> <ItemGroup Condition="$(IsTestProject) == 'true'"> <PackageVersion Include="FluentAssertions" Version="7.1.0" /> <PackageReference Include="FluentAssertions"> <NoWarn>NU1605</NoWarn> </PackageReference> </ItemGroup> </Project>
方法二:全局锁定版本约束
在解决方案根目录的NuGet.Config中添加全局版本约束,彻底阻止NuGet检查该包的更新:
<configuration> <packageVersionConstraints> <clear /> <add key="FluentAssertions" version="[7.1.0]" /> </packageVersionConstraints> </configuration>
3. 验证配置有效性
执行以下步骤确认配置生效:
- 清理解决方案:
dotnet clean - 还原依赖:
dotnet restore - 检查依赖版本:在测试项目目录运行
dotnet list package,确认FluentAssertions版本为7.1.0 - 打开NuGet包管理器,确认无该包的更新提示
内容的提问来源于stack exchange,提问作者Robert Schmidt
相关产品推荐
相关产品推荐

