F # and NUnit: structured output of record types when test fails

I use NUnit for unit test F # code, using the Resharper test runner inside Visual Studio 2013. I have several types of F # records and distinguishable connection types in projects and functions that return instances of these types, for example:

type MyRecord = { A : int; B : int }
let f () = { A = 4; B = 7 }

Now when I am writing a test case (using FSUnit) as shown below:

[<Test>] let ``test that always fails`` () = f () |> should be { A = 77; B = 99 }

I get a very useful output message in the lines "Expected: MyNamespace.MyRecord, but found: MyNamespace.MyRecord".

Of course, the problem is what NUnit uses object.ToString()to generate the output message, whereas I would use sprintf "%A" objectinstead to create a more useful output message like "Expected: {A = 77; B = 99}, but found: {A = 4 ; B = 7}. "

, ToString :

type MyRecord = { A : int; B : int } with override this.ToString () = sprintf "%A" this

, , don't-repeat-yourself . , F # 3.1.

, : NUnit ToString? - NUnit? NUnit, , . - , . , , F # , , - . .

, Unquote , . FSUnit Unquote. , Unquote: F # . -, , , . , , , .

, : , , ; InternalsVisibileTo, . , Unquote F #, , , .

?

+4
1

, NUnit:

let shouldEqual (x: 'T) (y: 'T) = 
    Assert.AreEqual(x, y, sprintf "Expected: %A\nActual: %A" x y)

, sprintf "%A" F #.

should equal. (. FsUnit` ` `Some []` Weird )

+4

Source: https://habr.com/ru/post/1544097/


All Articles