使用 List.map with 应该抛出异常

Using List.map with should throw Exception

let d5=["IIII";"VVVV";"XXXX";"LLLL";"MMMM"] 

[<Test; ExpectedException(typeof<System.Exception>)>]
    member this.
        ``More than 3 times repetive characters `` ()=
        //Missing part

此测试无效。 我有解析罗马数字的转换器。但它对

不起作用

["IIII";"VVVV";"XXXX";"LLLL";"MMMM"]

当你发送 convert("IIII") 时它 returns 4 ,但它应该给出 System.Exception 错误。我需要 编写 NUnit 测试 (不修复转换器),它映射列表中的每个字符串并在每个 returns 错误时通过。否则失败。 我要写的测试如下

d5=["IIII";"VVVV";"XXXX";"LLLL";"MMMM"]
convert each d5 element
if each of them is giving system.exception error 
then test is successfull
else test is failed

有什么解决办法吗?任何的想法。 . 如果你有兴趣转换方法在这里 Convert Method

您想要的是分别测试所有值:

open NUnit.Framework
open FsUnit

type ConvertTest() = 

    let convert s = 
        if String.length s > 3 then failwith "Something's wrong"
        else String.length s

    [<Theory>]
    member this.``More than 3 characters throws``([<Values("IIII", "VVVVVV", "XXXX", "LLLL", "MMMM")>] s) = 
        (fun () -> convert s |> ignore) |> should throw typeof<System.Exception>

    [<Theory>]
    member this.``Less than 4 characters returns length``([<Values("II", "VV", "XXX", "LLL", "MMM")>] s) = 
        convert s |> should equal s.Length

我特意将 convert 方法更改为略有不同的实现(如您提供的 none),但应该很明显如何继续。

您可以使用 TestCase 属性来参数化测试,并使用 Assert.Throws 来验证是否抛出了异常:

open System
open NUnit.Framework

[<TestFixture>]
type Tests() =

    [<TestCase("IIII")>]
    [<TestCase("VVVV")>]
    [<TestCase("XXXX")>]
    [<TestCase("LLLL")>]
    [<TestCase("MMMM")>]
    member this.ThrowsOnInvalidInput (cand : string) =
        Assert.Throws<Exception> (fun () -> convert cand |> ignore) |> ignore