F#:如何避免在测试项目中出现“[FS0988] 程序主模块为空:当它为 运行 时什么也不会发生”?

F#: How to avoid "[FS0988] Main module of program is empty: nothing will happen when it is run" in Tests Project?

我有一个包含以下项目的 .NET 解决方案:

我的Domain.Tests.fsproj定义为:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>

        <IsPackable>false</IsPackable>
        <GenerateProgramFile>false</GenerateProgramFile>
        <TargetFramework>netcoreapp2.2</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="FsCheck" Version="3.0.0-alpha4" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
        <PackageReference Include="xunit" Version="2.4.0" />
        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
    </ItemGroup>

    <ItemGroup>
      <Compile Include="Dsl.fs" />
      <Compile Include="OpenAccountTests.fs" />
      <Compile Include="CloseAccountTests.fs" />
      <Compile Include="DepositCashTests.fs" />
      <Compile Include="WithdrawCashTests.fs" />
      <Compile Include="WireMoneyTests.fs" />
      <Compile Include="RequestAddressChangeTests.fs" />
      <Compile Include="RequestEmailChangeTests.fs" />
      <Compile Include="RequestPhoneNumberChangeTests.fs" />
      <Compile Include="ValidateAddressChangeTests.fs" />
      <Compile Include="ValidateEmailChangeTests.fs" />
      <Compile Include="ValidatePhoneNumberChangeTests.fs" />
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\Domain\Domain.fsproj" />
    </ItemGroup>

</Project>

但是在编译解决方案时我有以下警告:

ValidatePhoneNumberChangeTests.fs(102, 35): [FS0988] Main module of program is empty: nothing will happen when it is run

我检查了 that answer on SO 并在 Domain.Tests 的最后一个文件的末尾添加了 do()ValidatePhoneNumberChangeTests.fs 没有做任何事情。

我该怎么做才能摆脱这个警告?

<TargetFramework>netcoreapp2.2</TargetFramework>

这将 Domain.Tests 项目指定为可执行文件

如果您只需要它是一个 class 库,则将其更改为

<TargetFramework>netstandard2.0</TargetFramework>

如果您只是想删除警告,您可以通过在 ValidatePhoneNumberChangeTests.fs 末尾添加此方法或在编译顺序

末尾的 Program.fs 中添加一个主要方法=]
[<EntryPoint>] 
let main argv =
    0

@rmunn 在评论中是在正确的轨道上。当 TargetFrameworknetstandardXXnet4XX 时,<OutputType> 默认为 Library,而当 TargetFrameworknetcoreappXX 时,Exe

设置 <OutputType>Library</OutputType> 是 IMO 解决此问题的最佳方法,而不是添加不会被调用的入口点。

只需删除 Program.fs 和 OutputType(它对我也不起作用)。像这样在 PropertyGroup 中将 GenerateProgramFile 设置为 true:

<PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <IsPackable>false</IsPackable>
    <GenerateProgramFile>true</GenerateProgramFile>
</PropertyGroup>

干杯!