I've been trying to write some F # code recently. However, I am accustomed to TDD, I feel a little uncomfortable, is not what is the disadvantage? Yes, the monomer test. After a search and investigation I decided to try Fsunit. The Learning notes for the sample code combined with this framework are as follows.
(* Add Reference *)
#r "FsUnit.NUnit.dll"
#r "Nunit.framework.dll"
(* Import namespaces *)
Open Nunit.framework
Open Fsunit
(* Definition of the tested class *)
Type lightbulb (state) =
member X.on = State
Override x.tostring () =
Match X.on with
| True-> "on"
| False-> "Off"
(*
First, a testfixture is defined by attribute.
When Setup generates a lightbulb instance, the initial state is true.
The testfixture contains two test methods that test the on property and the ToString method, respectively.
The naming of test methods is very distinctive. Test assertions are also very close to natural languages by using pipelines.
*)
[<testfixture>]
Type ' Given a lightbulb that has had its state set to True ' () =
Let lightbulb = new Lightbulb (true)
[<test>] member X.
' When I ask whether it's on it answers true. ' () =
Lightbulb.on |> should be True
[<test>] member X.
' When I convert it to a string it becomes ' on '. ' () =
String lightbulb |> should equal "on"
[<testfixture>]
Type ' Given a lightbulb that has had its state set to False ' () =
Let lightbulb = new Lightbulb (false)
[<test>] member X.
' I ask whether it's on it answers false ' () =
Lightbulb.on |> should be False
[<test>] member X.
' When I convert the it to a string it becomes. ' () =
String lightbulb |> should equal "off"
Fsunit is a xunit member that faces F #. With such a single test framework, we can write code more confidently.
SOURCE http://bj007.blog.51cto.com/1701577/347752