Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

20_WritingSpecifications

Thorsten Lorenz edited this page · 1 revision

Writing Specifications

Creating a specification project

  1. Create a new FSharp project inside Visual Studio
  2. Reference the FSharpSpec.dll
  3. Create an *.fs file which will contain your specifications

Writing the code

  1. Create a namespace inside your *.fs file and open the FSharpSpec namespace

    Example namespace MyFirstSpecs open FSharpSpec

  2. Create a type whose name describes the context, a member describing the action and one assertion - it

    Example type given a number that is zero () = let number = 0 member x.when i add one to it = let result = number + 1 it "results in one" result should.equal 1

    At this point you have a valid specification and can compile and run it.

    The it explained

    it is actually a function with the following signature: it [name of the specification] [actual] [assertion] [expected]

    In our example this maps to these values as follows: name: "results in one" actual: result assertion: should.equal expected: 1

  3. Add more assertions as needed

    FSharpSpec can handle single or multiple assertions equally except in certain cases as described in detail here. So, if you would like to specify more assertions you can do so, by returning a list of assertions (note the [ and ]). Example type given a number that is zero () = let number = 0 member x.when i add one to it = [ let result = number + 1 it "results in one" result should.equal 1 it "does not change the number" number should.equal 0 ]

  4. In order to add more specifications, just add more properties that return assertions as seen in the below example.

  5. In order to use a different context, create a new type, set up the context and add properties that return specifications.

Full example

namespace MyFirstSpecs
open FSharpSpec

type ``given a number that is zero`` () =
    let number = 0

    member x.``when i add one to it`` =  
        let result = number + 1
        [ 
            it "results in one" result should.equal 1 
            it "does not change the number" number should.equal 0
        ]

    member x.``when i add two to it`` =  
           let result = number + 2
           it "results in two" result should.equal 2

Runner output

+ given a number that is zero
|
   - when i add one to it
      -  results in one
      -  does not change the number


   - when i add two to it
      -  results in two

More Examples

FSharpSpec comes with lots of included examples that can be found here.

Something went wrong with that request. Please try again.