为什么在基于属性的测试中忽略了我的前提条件? [英] Why is my precondition being ignored on my Property-based test?

查看:93
本文介绍了为什么在基于属性的测试中忽略了我的前提条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是,我的测试仍然失败:

结果消息:System.Exception:可以伪造,经过3次测试(1 缩小)(StdGen(985619705,296133555)):原始:1 -1缩小:0 -1

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

[UPDATE]

我也尝试过:

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``

但是,我收到以下错误消息:

However, I receive the following error:

类型不匹配.期待一个 属性->'a,但给定一个 int-> Map<(int * int),Cell>类型"Property"与类型"int"不匹配

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.

As @FyodorSoikin points out in his comment, you have two nested functions that each take a rowCount.

second rowCount值遮盖了第一个值,但是==>前提条件函数仅对 first rowCount值起作用.因此,实际用于测试的rowCount值仍然没有限制.

The second rowCount value shadows the first one, but the ==> precondition function only works on the first rowCount value. Thus, the rowCount value actually used for testing is still unbounded.

使测试更简单,它将起作用:

Make the test simpler, and it'll work:

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)

这篇关于为什么在基于属性的测试中忽略了我的前提条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆