-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check if throws #128
Comments
Would it be possible to provide an example? |
This works, but feels rather heavy-handed: [<Fact>]
let ``throws if hour is < 0 or > 23`` () =
Property.check <| property {
let! dt = Gen.dateTime
let! hr = Range.linearBounded() |> Gen.int |> Gen.filter (fun hr -> hr < 0 || hr > 23)
try
nextOClock hr dt |> ignore
return false
with
:? ArgumentException -> return true
} |
Ideally there'd be some kind of [<Fact>]
let ``throws if hour is < 0 or > 23`` () =
Property.checkThrows<ArgumentException> <| property {
let! dt = Gen.dateTime
let! hr = Range.linearBounded() |> Gen.int |> Gen.filter (fun hr -> hr < 0 || hr > 23)
nextOClock hr dt |
What happens when you replace |
Oh, now I see what you mean; you want the exception to be thrown, and in this case, the property to pass. |
👍 |
@cmeeren, we recently added an internal function which does what you describe: fsharp-hedgehog/src/Hedgehog/Property.fs Lines 382 to 389 in 70d4454
AFAICT, this function does exactly what you need. If so, we can make it public in the next release. |
No, that does the opposite. It treats throws as failures. I want to test that a function does indeed throw an exception (the test should fail if nothing is thrown). Furthermore, it should check the type of exception (the test should fail if the wrong exception is thrown). And ideally, the user should be able to choose whether it needs to match the exception exactly or if a subclass of the specified exception is fine. IIRC FsCheck does this nicely. |
OK, then it's the opposite of that function plus pattern matching on the type of exception. |
Yes. See my example above for a suggestion of a nice and short syntax: Property.checkThrows<ArgumentException> I suggest that the default behavior is that the test passes if Property.checkThrowsExact<ArgumentException> |
Is it possible to check if a function throws an exception without having
try ... with
in the property and explicitly returningtrue
orfalse
?The text was updated successfully, but these errors were encountered: