Unit test for allowable checkers moves Is pattern matching within a unit test a code smell?
Do I really need to throw an exception just for my test to fail if the function under test receives invalid arguments (i.e. not Succeeded case value)?


Here's my test:

[&lt;Test&gt;]
let ``get available positions for black soldier`` () =

    // Setup
    let piece = Black ( BlackSoldier , (1,5) )

    // Test
    let available = startGame () |&gt; getPositions
                                 |&gt; optionsFor piece
    // Verify
    match available with
    | OptionsResponse.Succeeded coordinates -&gt;
        let northWestAvailable = coordinates |&gt; List.exists (fun pos -&gt; pos = (0,4))
        let northEastAvailable = coordinates |&gt; List.exists (fun pos -&gt; pos = (2,4))

        (northWestAvailable &amp;&amp; northEastAvailable) |&gt; should equal true

    | _ -&gt; failwith "Failed to get options"


Here's the function under test:

let optionsFor piece positions =

    let duplicatesFound = positions |&gt; Seq.countBy id
                                    |&gt; Seq.map snd
                                    |&gt; Seq.exists (fun count -&gt; count &gt; 1)
    if duplicatesFound
    then OptionsResponse.DuplicatesNotAllowed positions
    else match piece |&gt; isKing with
         | false -&gt; let options = positions |&gt; availablePositions 
                                            |&gt; List.filter (optionsForSoldier piece)
                    OptionsResponse.Succeeded options

         | true  -&gt; let options = positions |&gt; availablePositions 
                                            |&gt; List.filter (optionsForKing piece)
                    OptionsResponse.Succeeded options


Appendix:

type Coordinate = int * int

type Space =
    | Occupied  of Piece
    | Available of Coordinate

type OptionsResponse =
    | DuplicatesNotAllowed of Space list
    | Succeeded            of Coordinate list
Is pattern matching within a unit test a code smell?


I don't think so. When a tested method returns a discriminated union, then using pattern matching in the unit test is natural.


  Do I really need to throw an exception just for my test to fail if the function under test receives invalid arguments (i.e. not Succeeded case value)?


Yes, I think you should. The reason for that is that if the function incorrectly returns something other than Succeeded, your test needs to fail. And throwing an exception is how you do that.



Also, if you're a fan of terseness, you can write the List.exists predicate as e.g. ((=) (0,4)), instead of using fun:

let northWestAvailable = coordinates |&gt; List.exists ((=) (0,4))


Or, even better, use should contain:

coordinates |&gt; should contain (0,4)


This is shorter, clearer and will also lead to clearer test failures.