Scott Nimrod
Scott Nimrod

Reputation: 11595

Property-based test continues to pass when I expect it to fail

I'm sure this is trivial. However, the following test will always pass:

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

I've also tried:

Check.QuickThrowOnFailure <| (expected = actual)

Specifically, why does this test continue to pass when it should obviously fail?

Upvotes: 1

Views: 60

Answers (2)

Mark Seemann
Mark Seemann

Reputation: 233150

You can also use FsCheck.Xunit and its [<Property>] attribute:

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

Upvotes: 2

Fyodor Soikin
Fyodor Soikin

Reputation: 80744

You're creating the property, but never actually checking it. It just sits there, never executed once.

To check the property, you need to pass it to one of the Check.* methods:

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

Upvotes: 4

Related Questions