1

我敢肯定这是微不足道的。但是,以下测试将始终通过:

let transform number =  
    match number % 3, number % 5 with 
    | 0, 0 -> "FizzBuzz" 
    | _, 0 -> "Buzz" 
    | 0, _ -> "Fizz" 
    | _ -> number.ToString()

[<Fact>]
let ``FizzBuzz.transform returns FizzBuzz`` () =
    let fiveAndThrees = Arb.generate<int> |> Gen.map ((*) (3 * 5)) 
                                          |> Arb.fromGen

    Prop.forAll fiveAndThrees <| fun number ->

        let actual = transform number

        let expected = "jbdhjsdhjdsjhsdglsdjlljh"
        expected = actual

我也试过:

Check.QuickThrowOnFailure <| (expected = actual)

具体来说,为什么这个测试显然应该失败时仍然通过?

4

2 回答 2

4

您正在创建该属性,但从未真正检查它。它只是坐在那里,从未执行过一次。

要检查该属性,您需要将其传递给以下Check.*方法之一:

[<Fact>]
let ``FizzBuzz.transform returns FizzBuzz`` () =
    let fiveAndThrees = Arb.generate<int> |> Gen.map ((*) (3 * 5)) 
                                          |> Arb.fromGen

    let myProperty = Prop.forAll fiveAndThrees <| fun number ->

        let actual = transform number

        let expected = "jbdhjsdhjdsjhsdglsdjlljh"
        expected = actual

    Check.QuickThrowOnFailure myProperty
于 2016-03-25T02:27:43.713 回答
2

您还可以使用FsCheck.Xunit及其[<Property>]属性:

[<Property(QuietOnSuccess = true)>]
let ``FizzBuzz.transform returns FizzBuzz`` () =
    let fiveAndThrees =
        Arb.generate<int> |> Gen.map ((*) (3 * 5)) |> Arb.fromGen
    Prop.forAll fiveAndThrees <| fun number ->

        let actual = FizzBuzz.transform number

        let expected = "FizzBuzz"
        expected = actual
于 2016-03-25T11:01:05.247 回答