You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 21:07:06