How do I pass a function to NUnit Throws.Constraints?

Posted by Serge Belov on Stack Overflow See other posts from Stack Overflow or by Serge Belov
Published on 2012-12-12T04:28:33Z Indexed on 2012/12/12 5:03 UTC
Read the original article Hit count: 220

Filed under:
|
|

I'm trying to write some NUnit tests in F# and having trouble passing a function to the ThrowsConstraint. A distilled (non)working sample is below.

open System.IO
open NUnit.Framework

[<TestFixture>]
module Example =

    [<Test>]
    let foo() = 
        let f = fun () -> File.GetAttributes("non-existing.file")
        Assert.That(f, Throws.TypeOf<FileNotFoundException>())

This compiles just fine but I get the following from the NUnit test runner:

FsTest.Tests.Example.foo:
System.ArgumentException : The actual value must be a TestDelegate but was f@11
Parameter name: actual

While I'm able to work around the problem using ExpectedException attribute, my question is what is the correct way of using an F# function in this situation?

© Stack Overflow or respective owner

Related posts about F#

Related posts about interop