How do I pass a function to NUnit Throws.Constraints?
- by Serge Belov
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?