为什么我的前提条件在基于 属性 的测试中被忽略了?

Why is my precondition being ignored on my Property-based test?

为什么我的前提条件在基于 属性 的测试中被忽略了?

我测试的前提条件如下:

fun rowCount -> rowCount >= 0

因此,我的实际测试是:

[<Fact>]
let ``number of cells in grid equals rowcount squared`` () =
    Check.QuickThrowOnFailure <| 
            fun rowCount -> rowCount >= 0 ==>  
                            fun rowCount -> rowCount |> createGrid
                                                     |> Map.toList
                                                     |> List.length = rowCount * rowCount

然而,我的测试还是失败了:

Result Message: System.Exception : Falsifiable, after 3 tests (1 shrink) (StdGen (985619705,296133555)): Original: 1 -1 Shrunk: 0 -1

域:

let createGrid rowCount = 

    [for x in 0..rowCount-1 do
        for y in 0..rowCount-1 do
            yield { X=x; Y=y; State=Dead } 
    ]|> List.map (fun c -> (c.X, c.Y), { X=c.X; Y=c.Y; State=Dead })
     |> Map.ofList

[更新]

我也试过:

let precondition rowCount =
    rowCount >= 0

let ``some property`` rowCount = 

    precondition rowCount ==> rowCount |> createGrid 
                                       |> Map.toList
                                       |> List.length = rowCount * rowCount
[<Fact>]
let ``number of cells in grid equals rowcount squared`` () =
    Check.QuickThrowOnFailure <| ``some property``

但是,我收到以下错误:

Type mismatch. Expecting a Property -> 'a but given a int -> Map<(int * int),Cell> The type 'Property' does not match the type 'int'

正如@FyodorSoikin 在他的评论中指出的那样,您有两个嵌套函数,每个函数都采用 rowCount.

second rowCount 值隐藏第一个,但 ==> 前置条件函数仅适用于 first rowCount 值。因此,实际用于测试的rowCount值仍然是无界的。

让测试更简单,它就会成功:

open Xunit
open FsCheck

[<Fact>]
let ``number of cells in grid equals rowcount squared`` () =
    Check.QuickThrowOnFailure <| fun rowCount ->
        rowCount >= 0 ==>  
        (rowCount
            |> createGrid
            |> Map.toList
            |> List.length = rowCount * rowCount)