Skip to content

Commit f4f352f

Browse files
authored
Merge pull request #37 from jkone27/try-add-csharp-extensions
WIP: try add csharp extensions, to fix
2 parents 9a4a497 + 3930bd6 commit f4f352f

File tree

6 files changed

+52
-35
lines changed

6 files changed

+52
-35
lines changed

ApiStub.FSharp/ApiStub.FSharp.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>net6.0</TargetFramework>
44
<GenerateDocumentationFile>true</GenerateDocumentationFile>
55
<PackageId>ApiStub.FSharp</PackageId>
6-
<Version>1.0.2</Version>
6+
<Version>1.1.0</Version>
77
<Authors>jkone27</Authors>
88
<RequireLicenseAcceptance>false</RequireLicenseAcceptance>
99
<PackageLicenseExpression>MIT</PackageLicenseExpression>
@@ -25,6 +25,7 @@
2525
<Compile Include="HttpResponseHelpers.fs" />
2626
<Compile Include="DelegatingHandlers.fs" />
2727
<Compile Include="CE.fs" />
28+
<Compile Include="Csharp.fs" />
2829
<Compile Include="BDD.fs" />
2930
</ItemGroup>
3031
<ItemGroup>

ApiStub.FSharp/Csharp.fs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace ApiStub.Fsharp
2+
3+
open System.Runtime.CompilerServices
4+
open ApiStub.FSharp.CE
5+
6+
[<Extension>]
7+
type CsharpExtensions =
8+
9+
[<Extension>]
10+
static member GETJ<'a when 'a: not struct>(x: TestClient<'a>, route: string, stub: obj) = x.GetJson(x, route, stub)
11+
12+
[<Extension>]
13+
static member POSTJ<'a when 'a: not struct>(x: TestClient<'a>, route: string, stub: obj) =
14+
x.PostJson(x, route, stub)
15+
16+
[<Extension>]
17+
static member PUTJ<'a when 'a: not struct>(x: TestClient<'a>, route: string, stub: obj) = x.PutJson(x, route, stub)
18+
19+
[<Extension>]
20+
static member DELETEJ<'a when 'a: not struct>(x: TestClient<'a>, route: string, stub: obj) =
21+
x.DeleteJson(x, route, stub)

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ module Tests =
7878
}
7979
```
8080

81+
## C# APIs
82+
83+
if you prefer to use C# some extension methods are provided to use with C#.
84+
85+
`GETJ, PUTJ, POSTJ, DELETEJ`
86+
87+
If you want to access more overloads, you can access the inspect `TestClient<T>` members and create your custom extension methods easilly.
88+
89+
90+
```csharp
91+
using ApiStub.FSharp;
92+
using static ApiStub.Fsharp.CsharpExtensions;
93+
94+
var webAppFactory = new CE.TestClient<Web.Sample.Program>()
95+
.GETJ(Clients.Routes.name, new { Name = "Peter" })
96+
.GETJ(Clients.Routes.age, new { Age = 100 })
97+
.GetFactory();
98+
99+
// factory.CreateClient(); // as needed later in your tests
100+
101+
```
102+
81103
## HTTP Methods 🚕
82104

83105
Available HTTP methods in the test dsl to "mock" HTTP client responses are the following:

samples/web/test/Web.Sample.Csharp.Test/CSharpTests.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,21 @@
22

33
using System;
44
using Xunit;
5-
using ApiStub.FSharp;
65
using System.Threading.Tasks;
7-
using static ApiStub.FSharp.CE;
86
using Web.Sample;
97
using System.Net.Http.Json;
108
using Microsoft.AspNetCore.Mvc.Testing;
9+
using static ApiStub.FSharp.CE;
10+
using static ApiStub.Fsharp.CsharpExtensions;
1111

1212
public class CSharpTests
1313
{
14-
private static WebApplicationFactory<Web.Sample.Program> getWebAppFactory()
15-
{
14+
private static WebApplicationFactory<Web.Sample.Program> getWebAppFactory() =>
1615
// create an instance of the test client builder
17-
var b = new TestClient<Web.Sample.Program>();
18-
19-
return
20-
b.GetJson(b, Clients.Routes.name, new { Name = "Peter" })
21-
.GetJson(b, Clients.Routes.age, new { Age = 100 })
16+
new TestClient<Web.Sample.Program>()
17+
.GETJ(Clients.Routes.name, new { Name = "Peter" })
18+
.GETJ(Clients.Routes.age, new { Age = 100 })
2219
.GetFactory();
23-
}
2420

2521
// one app factory instance is oke for all tests
2622
private static readonly WebApplicationFactory<Program> webAppFactory = getWebAppFactory();

samples/web/test/Web.Sample.Csharp.Test/Web.Sample.Csharp.Test.sln

Lines changed: 0 additions & 24 deletions
This file was deleted.

samples/web/test/Web.Sample.Test/Tests.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ open ApiStub.FSharp.CE
88
open Web.Sample
99
open System.Net.Http.Json
1010

11+
1112
let webAppFactory =
1213
new TestClient<Web.Sample.Program>()
1314
|> fun x -> x {

0 commit comments

Comments
 (0)