Parameterized tests in f # is not a valid constant expression

For some reason, when passing arguments to the test through TestCaseattrubute, I get the following error message about the first argument, which in this case is an array:

This is not a valid constant expression or custom attribute value.

module GameLogicTest = 
    open FsUnit
    open NUnit.Framework
    open GameLogic.Examle

    // This is not a valid constant expression or custom attribute value
    [<TestCase( [| 1; 2; 3 |], 3, 1,1)>]
    let ``let example.`` (a, m, h, c) = 
        a
        |> proof1 m
        |> should equal (h,c)

But when deleting the last argument, both from the attribute itself and from the method itself, everything works fine.

[<TestCase( [| 1; 2; 3 |], 3, 1)>]
let ``let example.`` (a, m, h) = 
    a
    |> proof1 m
    |> should equal (h,1)

What am I doing wrong? Preferably, I also defined the tuple int * int, but it doesn't work either.

+4
source share
2 answers

The CLI has a limitation regarding attribute parameters:

  • : bool, int, float ..
  • : System.Type
  • 'kinda objects': ( )
  • (.. )

, , .

, .

, F # , (params). ,

public class ParamsAttribute : Attribute
{
    public ParamsAttribute(params object[] parameters)
    {}
}

F #, :

[<Params(1, 2, 3)>] // here everything is OK
let x () = ()

[<Params([|1; 2; 3|])>] // the same error as you have
let y () = ()

, params , , "" . , , .

+5

. . String.Split Array.map .

[<TestCase([|1, 2, 3|], 4, 5, 6)>]
let ``My Test``(param1: int[], param2: int, param3: int, param4: int) =
    // My test code

[<TestCase("1|2|3", 4, 5, 6)>]
let ``My Test``(param1: string, param2: int, param3: int, param4: int) =
    let stringArray = param1.Split('|', SplitStringOptions.None)
    let intArray = Array.map int stringArray

    // My test code
+1

All Articles