Skip to content

Commit 7c920b3

Browse files
authored
Merge pull request #68 from jkone27/minor-fixes-docs-and-names
feat: Renamed TestClient to TestWebAppFactoryBuilder
2 parents 93b39e5 + a3a90a1 commit 7c920b3

File tree

12 files changed

+119
-100
lines changed

12 files changed

+119
-100
lines changed

.github/workflows/publish.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
name: Publish
22

33
on:
4-
push:
5-
branches:
4+
# trigger only on completion of Build on main
5+
workflow_run:
6+
workflows: ["Build"]
7+
branches:
68
- main
9+
types:
10+
- completed
711

812
jobs:
913
publish:
@@ -29,8 +33,7 @@ jobs:
2933

3034
- name: Run Versionize
3135
id: versionize
32-
run: |
33-
dotnet versionize
36+
run: dotnet versionize
3437
continue-on-error: true
3538

3639
- name: No release required

ApiStub.FSharp/BDD.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module BDD =
1313
/// Defines a BDD scenario
1414
type Scenario<'TStartup when 'TStartup: not struct> =
1515
{ UseCase: string
16-
TestClient: TestClient<'TStartup> }
16+
TestWAFBuilder: TestWebAppFactoryBuilder<'TStartup> }
1717

1818
/// Defines the context propagated through the test
1919
type Environment<'TStartup, 'FeatureStubData when 'TStartup: not struct> =
@@ -42,9 +42,9 @@ module BDD =
4242

4343

4444
/// Scenario builder
45-
let SCENARIO useCase testClient =
45+
let SCENARIO useCase (builder: TestWebAppFactoryBuilder<_>) =
4646
{ UseCase = useCase
47-
TestClient = testClient }
47+
TestWAFBuilder = builder }
4848
|> Step.Scenario
4949

5050
/// Setup the Environment for the given scenario
@@ -152,17 +152,17 @@ module BDD =
152152

153153

154154
// [<Fact>] sample
155-
// let ``when i call /hello i get 'world' back with 200 ok`` (testClient: TestClient<_>) =
155+
// let ``when i call /hello i get 'world' back with 200 ok`` (TestWebAppFactoryBuilder: TestWebAppFactoryBuilder<_>) =
156156

157157
// let stubData = [ 1, 2, 3 ]
158158

159-
// testClient { GET "/hello" (fun _ _ -> $"hello world {stubData}" |> R_TEXT) }
159+
// TestWebAppFactoryBuilder { GET "/hello" (fun _ _ -> $"hello world {stubData}" |> R_TEXT) }
160160
// |> SCENARIO "when i call /hello i get 'world' back with 200 ok"
161161
// |> SETUP
162162
// (fun s ->
163163
// task {
164164

165-
// let test = s.TestClient
165+
// let test = s.TestWebAppFactoryBuilder
166166

167167
// let f = test.GetFactory()
168168

ApiStub.FSharp/CE.fs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ open Microsoft.Extensions.Http
99
open Microsoft.AspNetCore.Routing.Template
1010
open Microsoft.AspNetCore.Routing
1111

12-
/// computation expression module (builder CE), contains `TestClient<T>` that wraps `WebApplicationFactory<T>`
12+
/// computation expression module (builder CE), contains `TestWebAppFactoryBuilder` (former `TestWebAppFactoryBuilder`) that wraps `WebApplicationFactory`
1313
module CE =
1414
open BuilderExtensions
1515
open HttpResponseHelpers
@@ -18,8 +18,8 @@ module CE =
1818
let private toAsync stub =
1919
fun req args -> task { return stub req args }
2020

21-
/// `TestClient<T>` wraps `WebApplicationFactory<T>` and exposes a builder CE with utility to define api client stubs and other features
22-
type TestClient<'T when 'T: not struct>() =
21+
/// `TestWebAppFactoryBuilder` wraps `WebApplicationFactory` and exposes a builder CE with utility to define api client stubs and other features
22+
type TestWebAppFactoryBuilder<'T when 'T: not struct>() =
2323

2424
let factory = new WebApplicationFactory<'T>()
2525
let mutable httpMessageHandler: DelegatingHandler = null
@@ -82,22 +82,14 @@ module CE =
8282

8383
[<CustomOperation("stub")>]
8484
member this.Stub
85-
(
86-
x,
87-
methods,
88-
routeTemplate,
89-
stub: HttpRequestMessage -> RouteValueDictionary -> HttpResponseMessage
90-
) =
85+
(x, methods, routeTemplate, stub: HttpRequestMessage -> RouteValueDictionary -> HttpResponseMessage)
86+
=
9187
this.StubWithOptions(x, methods, routeTemplate, stub |> toAsync, false)
9288

9389
[<CustomOperation("stub_async")>]
9490
member this.StubAsync
95-
(
96-
x,
97-
methods,
98-
routeTemplate,
99-
stub: HttpRequestMessage -> RouteValueDictionary -> HttpResponseMessage Task
100-
) =
91+
(x, methods, routeTemplate, stub: HttpRequestMessage -> RouteValueDictionary -> HttpResponseMessage Task)
92+
=
10193
this.StubWithOptions(x, methods, routeTemplate, stub, false)
10294

10395
/// stub operation with stub object (HttpResponseMessage)
@@ -232,3 +224,7 @@ module CE =
232224
|> web_configure_test_services (fun s ->
233225
for custom_config in customConfigureTestServices do
234226
custom_config (s) |> ignore)
227+
228+
/// `TestClient` is a type backfill for `TestWebAppFactoryBuilder`, please switch to the new name if possible
229+
[<Obsolete("Use TestWebAppFactoryBuilder type instead")>]
230+
type TestClient<'T when 'T: not struct> = TestWebAppFactoryBuilder<'T>

ApiStub.FSharp/Csharp.fs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ open ApiStub.FSharp.CE
77
type CsharpExtensions =
88

99
[<Extension>]
10-
static member GETJ<'a when 'a: not struct>(x: TestClient<'a>, route: string, stub: obj) = x.GetJson(x, route, stub)
10+
static member GETJ<'a when 'a: not struct>(x: TestWebAppFactoryBuilder<'a>, route: string, stub: obj) =
11+
x.GetJson(x, route, stub)
1112

1213
[<Extension>]
13-
static member POSTJ<'a when 'a: not struct>(x: TestClient<'a>, route: string, stub: obj) =
14+
static member POSTJ<'a when 'a: not struct>(x: TestWebAppFactoryBuilder<'a>, route: string, stub: obj) =
1415
x.PostJson(x, route, stub)
1516

1617
[<Extension>]
17-
static member PUTJ<'a when 'a: not struct>(x: TestClient<'a>, route: string, stub: obj) = x.PutJson(x, route, stub)
18+
static member PUTJ<'a when 'a: not struct>(x: TestWebAppFactoryBuilder<'a>, route: string, stub: obj) =
19+
x.PutJson(x, route, stub)
1820

1921
[<Extension>]
20-
static member DELETEJ<'a when 'a: not struct>(x: TestClient<'a>, route: string, stub: obj) =
22+
static member DELETEJ<'a when 'a: not struct>(x: TestWebAppFactoryBuilder<'a>, route: string, stub: obj) =
2123
x.DeleteJson(x, route, stub)
2224

2325
[<Extension>]
24-
static member PATCHJ<'a when 'a: not struct>(x: TestClient<'a>, route: string, stub: obj) =
26+
static member PATCHJ<'a when 'a: not struct>(x: TestWebAppFactoryBuilder<'a>, route: string, stub: obj) =
2527
x.PatchJson(x, route, stub)

README.md

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ Access the [documentation website](https://jkone27.github.io/fsharp-integration-
2020

2121
```mermaid
2222
sequenceDiagram
23-
participant TestClient as Test
23+
participant TestWebAppFactoryBuilder as Test
2424
participant MainApp as App
2525
participant DependencyApp as Dep
2626
27-
TestClient->>MainApp: GET /Hello
27+
TestWebAppFactoryBuilder->>MainApp: GET /Hello
2828
MainApp->>DependencyApp: GET /externalApi
2929
DependencyApp-->>MainApp: Response
30-
MainApp-->>TestClient: Response
30+
MainApp-->>TestWebAppFactoryBuilder: Response
3131
3232
```
3333

@@ -44,45 +44,49 @@ open Xunit
4444
module Tests =
4545
4646
// build your aspnetcore integration testing CE
47-
let test = new TestClient<Program>()
47+
let test = new TestWebAppFactoryBuilder<Program>()
4848
4949
[<Fact>]
50-
let ``Calls Hello and returns OK`` () =
50+
let ``Calls Hello and returns OK`` () = task {
5151
52-
task {
52+
let client =
53+
test {
54+
GETJ "/externalApi" {| Ok = "yeah" |}
55+
}
56+
|> _.GetFactory()
57+
|> _.CreateClient()
5358
54-
let client =
55-
test {
56-
GETJ "/externalApi" {| Ok = "yeah" |}
57-
}
58-
|> _.GetFactory()
59-
|> _.CreateClient()
59+
let! r = client.GetAsync("/Hello")
6060
61-
let! r = client.GetAsync("/Hello")
61+
// rest of your tests...
6262
63-
// rest of your tests...
64-
65-
}
63+
}
6664
```
6765

6866
or in `C#` if you prefer
6967

7068
```csharp
7169
using ApiStub.FSharp;
70+
using Xunit;
7271
using static ApiStub.Fsharp.CsharpExtensions;
7372

74-
async Task CallsHelloAndReturnsOk () {
75-
76-
var client =
77-
new CE.TestClient<Web.Sample.Program>()
78-
.GETJ("/externalApi", new { Ok = "Yeah" })
79-
.GetFactory()
80-
.CreateClient();
73+
public class Tests
74+
{
75+
[Fact]
76+
async Task CallsHelloAndReturnsOk()
77+
{
8178

82-
var r = await client.GetAsync("/Hello");
79+
var client =
80+
new CE.TestWebAppFactoryBuilder<Web.Sample.Program>()
81+
.GETJ("/externalApi", new { Ok = "Yeah" })
82+
.GetFactory()
83+
.CreateClient();
8384

84-
// rest of your tests...
85+
var r = await client.GetAsync("/Hello");
8586

87+
// rest of your tests...
88+
}
89+
}
8690
```
8791

8892
### Test .NET C# 🤝 from F#

0 commit comments

Comments
 (0)