2016-03-22 2 views
1

Pourquoi ma condition préalable est-elle ignorée dans mon test basé sur la propriété?Pourquoi ma condition préalable est-elle ignorée dans mon test basé sur la propriété?

La condition sine qua non pour mon test est la suivante:

fun rowCount -> rowCount >= 0 

Ainsi, mon test réel est:

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

Cependant, mon test échoue:

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

Domaine:

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]

J'ai aussi essayé:

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

Cependant, je reçois l'erreur suivante:

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

+0

Votre propriété prend _two_ arguments - '' rowCount' et seconde rowCount', - pas. –

+0

Je suis confus: laissez '' some property'' rowCount = ... –

Répondre

3

Comme @FyodorSoikin le signale dans son commentaire, vous avez deux fonctions imbriquées qui prennent chacune un rowCount.

La secondes valeur rowCount ombre la première, mais la fonction de condition ==> ne fonctionne que sur la première valeur rowCount. Ainsi, la valeur rowCount réellement utilisée pour le test est encore illimitée.

Faire le test plus simple, et ça va marcher:

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)