Skip to content

Instantly share code, notes, and snippets.

@AlexeyRaga
Created April 17, 2022 12:27
Show Gist options
  • Save AlexeyRaga/a40532087622c580ca10b1ef8432bd8e to your computer and use it in GitHub Desktop.
Save AlexeyRaga/a40532087622c580ca10b1ef8432bd8e to your computer and use it in GitHub Desktop.
Given-When-Then Test "framework"
[<Microsoft.FSharp.Core.AutoOpen>]
module TestFramework
open EP.EventSourcing.Aggregates
open Xunit
type Given<'Cmd, 'Res> = private Given of 'Cmd * 'Res
type TestBuilder<'State, 'Command, 'Event, 'Error>
( aggregate : Aggregate<'State, 'Command, 'Event, 'Error>,
aid : Id<'State> ) =
let runTest (Given (command, events)) =
events |> Seq.fold aggregate.Apply (aggregate.Create aid) |> aggregate.Execute command
member this.Yield _ = Given((), ())
[<CustomOperation("given", MaintainsVariableSpace = true)>]
member this.Given(Given ((), ()), events : #seq<'Event>) = Given((), Seq.toList events)
[<CustomOperation("executing", MaintainsVariableSpace = true)>]
member this.When(Given ((), events : 'Event list), command) = Given(command, events)
[<CustomOperation("shouldYield", MaintainsVariableSpace = true)>]
member this.ShouldYield(given, expectedEvents : #seq<'Event>) =
Assert.Equal(Ok(Array.ofSeq expectedEvents), runTest given)
[<CustomOperation("shouldError")>]
member this.ShouldError(given, expectedFailure : 'Error) =
Assert.Equal(Error expectedFailure, runTest given)
let test aggregate aggregateId = TestBuilder(aggregate, aggregateId)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment