Search code examples
f#xunitfscheck

FsCheck.xUnit: testing types from another assembly


I'm trying to make my first test with FsCheck and xUnit. I have the following setup:

  • An F# class library with a single file

    namespace Lib
    type ABC = A | B | C
    
  • A project called Tests with a reference to FsCheck.xUnit NuGet package with a single file:

    module LibTests    
    open FsCheck.Xunit
    open Lib    
    [<Property>]
    let ``ABC is always A`` v =
      v = A
    

When I run the project (with VS or console runner), I get the following error:

System.Exception : The type Lib.ABC is not handled automatically by FsCheck. 
Consider using another type or writing and registering a generator for it

If I move my type to the Tests assembly, everything works fine (test fails). How do I tests external types?


Solution

  • It appears to be related to a known issue described on FsCheck GitHub.

    Basically, FsCheck NuGet package depends on the old version of FSharp.Core library. The old version gets referenced, which makes the test code incompatible to the system under test.

    To fix the problem:

    1. After you install FsCheck NuGet package, go to test project references and remove the reference to the old version of FSharp.Core (4.3.1.0 in my case).

    2. Click "Add reference" to add it again, go to Assemblies -> Extensions and add FSharpCore of the same version that is used in your other projects, 4.4.0.0 in my case.

    3. Add an App.config file to your test project with the following contents:

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
        <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
              <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
              <bindingRedirect oldVersion="4.3.1.0" newVersion="4.4.0.0" />
            </dependentAssembly>
          </assemblyBinding>
        </runtime>
      </configuration>