Repository navigation
Expand file tree
/
Copy pathProductControllerTest.cs
More file actions
97 lines (83 loc) · 3.59 KB
/
Copy pathProductControllerTest.cs
File metadata and controls
97 lines (83 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using UnitTestEx.Api;
using UnitTestEx.Api.Controllers;
using Xunit;
using Xunit.Abstractions;
namespace UnitTestEx.Xunit.Test
{
public class ProductControllerTest : WithApiTester<Startup>, IClassFixture<ProductApiTestFixture<Startup>>
{
public ProductControllerTest(ProductApiTestFixture<Startup> fixture, ITestOutputHelper output) : base(fixture, output) { }
[Fact]
public void Notfound()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys/"))
.Request(HttpMethod.Get, "products/xyz").Respond.With(HttpStatusCode.NotFound);
Test.ReplaceHttpClientFactory(mcf)
.Controller<ProductController>()
.Run(c => c.Get("xyz"))
.AssertNotFound();
}
[Fact]
public void Success()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Get, "products/abc").Respond.WithJson(new { id = "Abc", description = "A blue carrot" });
Test.ReplaceHttpClientFactory(mcf)
.Controller<ProductController>()
.Run(c => c.Get("abc"))
.AssertOK()
.AssertValue(new { id = "Abc", description = "A blue carrot" });
}
[Fact]
public async Task ServiceProvider()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys")).Request(HttpMethod.Get, "test").Respond.With("test output");
var hc = Test.ReplaceHttpClientFactory(mcf)
.Services.GetService<IHttpClientFactory>().CreateClient("XXX");
var r = await hc.GetAsync("test");
Assert.NotNull(r);
Assert.Equal("test output", await r.Content.ReadAsStringAsync());
}
[Fact]
public async Task HttpMock_SharedInterface_ConfiguresIdenticallyAcrossTiers()
{
// Proves that the exact same test-authoring code (HttpMockSharedConfig.ConfigureProductStubAsync, written once against IHttpMockClient) can configure Tier 1's
// MockHttpClient identically to Tier 2/3's AspireHttpMockClient (see the equivalent test in UnitTestEx.Aspire.Xunit.Test).
var mcf = MockHttpClientFactory.Create();
var client = mcf.CreateClient("XXX", new Uri("https://somesys/"));
var stub = await HttpMockSharedConfig.ConfigureProductStubAsync(client, "products/shared");
Test.ReplaceHttpClientFactory(mcf)
.Controller<ProductController>()
.Run(c => c.Get("shared"))
.AssertOK()
.AssertValue(new { id = "Shared", description = "Configured via the shared IHttpMockClient interface." });
await stub.VerifyAsync();
}
[Fact]
public void To_HttpResponseMessage_Created()
{
Test.Type<ProductController>()
.Run(c => c.GetCreated())
.ToHttpResponseMessageAssertor()
.AssertCreated()
.AssertLocationHeaderContains("bananas")
.AssertContent("abc");
}
[Fact]
public void To_HttpResponseMessage_OK()
{
Test.Type<ProductController>()
.Run(c => c.GetOK())
.ToHttpResponseMessageAssertor()
.AssertOK();
}
}
}