Etibar - a tea bar
Etibar - a tea bar

Reputation: 1962

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

This test isn't working. I have converter which parsers Rome numbers. But it works wrong for

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

When you send convert("IIII") it returns 4 , but it should have given System.Exception error. I need to write NUnit test (not fix converter) which maps every string in the list and passes when every single of them returns error. Otherwise fails. The test I want to write is as following.

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

Any solution? Any idea. . If you are interested convert method is here Convert Method

Upvotes: 1

Views: 138

Answers (2)

Mark Seemann
Mark Seemann

Reputation: 233150

You can use TestCase attributes to parameterize the test, and Assert.Throws to verify that an exception was thrown:

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

Upvotes: 2

CaringDev
CaringDev

Reputation: 8551

What you want is to test all values separately:

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

I deliberately changed the convert method to a slightly different implementation (as you gave none) but it should be obvious how to proceed.

Upvotes: 1

Related Questions