1 people like it.

xBehave Quickstart sample with custom operator

xBehave Quickstart sample conversion in F# using a custom operator: https://github.com/xbehave/xbehave.net/wiki/Quickstart https://github.com/xbehave/xbehave.net/wiki/Scenarios-with-examples

SUT

1: 
type Calculator () = member __.Add(x,y) = x + y

Quickstart

1: 
2: 
3: 
4: 
5: 
6: 
7: 
let [<Scenario>] addition(x:int,y:int,calculator:Calculator,answer:int) =
    let x,y,calculator,answer = ref x, ref y, ref calculator, ref answer
    "Given the number 1" --> fun () -> x := 1
    "And the number 2" --> fun () -> y := 2
    "And a calculator" --> fun () -> calculator := Calculator()
    "When I add the numbers together" --> fun () -> answer := (!calculator).Add(!x, !y)
    "Then the answer is 3" --> fun () -> Assert.Equal(3, !answer)

Scenarios with examples

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
[<Scenario>]
[<Example(1, 2, 3)>]
[<Example(2, 3, 5)>]
let adding(x:int,y:int,expectedAnswer:int,calculator:Calculator,answer:int) =
    let calculator, answer = ref calculator, ref answer
    "Given the number {0}" --> id   
    "And the number {1}" --> id
    "And a calculator" --> fun () -> calculator := Calculator()
    "When I add the numbers together" --> fun () -> answer := (!calculator).Add(x, y)
    "Then the answer is {2}" --> fun () -> Assert.Equal(expectedAnswer, !answer)
Multiple items
type Calculator =
  new : unit -> Calculator
  member Add : x:int * y:int -> int

Full name: Script.Calculator

--------------------
new : unit -> Calculator
member Calculator.Add : x:int * y:int -> int

Full name: Script.Calculator.Add
val x : int
val y : int
val addition : x:int * y:int * calculator:Calculator * answer:int -> unit

Full name: Script.addition
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val calculator : Calculator
val answer : int
val x : int ref
val y : int ref
val calculator : Calculator ref
val answer : int ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val adding : x:int * y:int * expectedAnswer:int * calculator:Calculator * answer:int -> unit

Full name: Script.adding
val expectedAnswer : int
val id : x:'T -> 'T

Full name: Microsoft.FSharp.Core.Operators.id

More information

Link:http://fssnip.net/ko
Posted:3 years ago
Author:Phillip Trelford
Tags: xbehave , bdd