-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProductControllerTest.cs
More file actions
78 lines (68 loc) · 2.62 KB
/
ProductControllerTest.cs
File metadata and controls
78 lines (68 loc) · 2.62 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
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 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();
}
}
}