|
| 1 | +(*** hide ***) |
| 2 | +#r "nuget: FSharp.Formatting, 11.3.0" |
| 3 | + |
| 4 | +(** |
| 5 | +## BDD (gherkin) Extensions 🥒 |
| 6 | +
|
| 7 | +You can use some BDD extension to perform [Gherkin-like setups and assertions](https://cucumber.io/docs/gherkin/reference/) |
| 8 | +
|
| 9 | +they are all async `task` computations so they can be simply chained together: |
| 10 | +
|
| 11 | +* `SCENARIO`: takes a `TestClient<TStartup>` as input and needs a name for your test scenario |
| 12 | +* `SETUP`: takes a scenario as input and can be used to configure the "test environmenttest": factory and the API client, additionally takes a separate API client configuration |
| 13 | +* `GIVEN`: takes a "test environment" or another "given" and returns a "given" step |
| 14 | +* `WHEN`: takes a "given" or another "when" step, and returns a a "when" step |
| 15 | +* `THEN`: takes a "when" step and asserts on it, returns the same "when" step as result to continue asserts |
| 16 | +* `END`: disposes the "test environment" and concludes the task computation |
| 17 | +
|
| 18 | +```fsharp |
| 19 | +// open ... |
| 20 | +open ApiStub.FSharp.BDD |
| 21 | +open HttpResponseMessageExtensions |
| 22 | +
|
| 23 | +module BDDTests = |
| 24 | +
|
| 25 | + let testce = new TestClient<Startup>() |
| 26 | +
|
| 27 | + [<Fact>] |
| 28 | + let ``when i call /hello i get 'world' back with 200 ok`` () = |
| 29 | + |
| 30 | + let mutable expected = "_" |
| 31 | + let stubData = { Ok = "undefined" } |
| 32 | +
|
| 33 | + // ARRANGE step is divided in CE (arrange client stubs) |
| 34 | + // SETUP: additional factory or service or client configuration |
| 35 | + // and GIVEN the actual arrange for the test 3As. |
| 36 | + |
| 37 | + // setup your test as usual here, test_ce is an instance of TestClient<TStartup>() |
| 38 | + test_ce { |
| 39 | + POSTJ "/another/anotherApi" {| Test = "NOT_USED_VAL" |} |
| 40 | + GET_ASYNC "/externalApi" (fun r _ -> task { |
| 41 | + return { stubData with Ok = expected } |> R_JSON |
| 42 | + }) |
| 43 | + } |
| 44 | + |> SCENARIO "when i call /Hello i get 'world' back with 200 ok" |
| 45 | + |> SETUP (fun s -> task { |
| 46 | + |
| 47 | + let test = s.TestClient |
| 48 | + |
| 49 | + // any additiona services or factory configuration before this point |
| 50 | + let f = test.GetFactory() |
| 51 | + |
| 52 | + return { |
| 53 | + Client = f.CreateClient() |
| 54 | + Factory = f |
| 55 | + Scenario = s |
| 56 | + FeatureStubData = stubData |
| 57 | + } |
| 58 | + }) (fun c -> c) // configure test client here if needed |
| 59 | + |> GIVEN (fun g -> //ArrangeData |
| 60 | + expected <- "world" |
| 61 | + expected |> Task.FromResult |
| 62 | + ) |
| 63 | + |> WHEN (fun g -> task { //ACT and AssertData |
| 64 | + let! (r : HttpResponseMessage) = g.Environment.Client.GetAsync("/Hello") |
| 65 | + return! r.Content.ReadFromJsonAsync<Hello>() |
| 66 | +
|
| 67 | + }) |
| 68 | + |> THEN (fun w -> // ASSERT |
| 69 | + Assert.Equal(w.Given.ArrangeData, w.AssertData.Ok) |
| 70 | + ) |
| 71 | + |> END |
| 72 | +
|
| 73 | +``` |
0 commit comments