generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProductControllerTest.cs
134 lines (116 loc) · 4.8 KB
/
ProductControllerTest.cs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using UnitTestEx.Api;
using UnitTestEx.Api.Controllers;
using UnitTestEx.Expectations;
namespace UnitTestEx.NUnit.Test
{
[TestFixture]
public class ProductControllerTest
{
public ProductControllerTest()
{
TestSetUp.Default.RegisterAutoSetUp((_, __, ___) =>
{
return Task.FromResult((true, "Some real magic happened here!"));
});
}
[OneTimeSetUp]
public void OneTimeSetUp()
{
var uti = NUnit.Internal.NUnitTestImplementor.Create();
uti.WriteLine("ONE-TIME-SETUP"); // This does not work; bug of test framework.
System.Diagnostics.Debug.WriteLine("ONE-TIME-SETUP");
TestSetUp.Default.SetUp();
}
[Test]
public void Notfound()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys/"))
.Request(HttpMethod.Get, "products/xyz").Respond.With(HttpStatusCode.NotFound);
using var test = ApiTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.Controller<ProductController>()
.Run(c => c.Get("xyz"))
.AssertNotFound();
}
[Test]
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" });
using var test = ApiTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.Controller<ProductController>()
.Run(c => c.Get("abc"))
.AssertOK()
.AssertValue(new { id = "Abc", description = "A blue carrot" });
}
[Test]
public void Success2()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Get, "products/xyz").Respond.WithJson(new { id = "Xyz", description = "Xtra yellow elephant" });
using var test = ApiTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.Controller<ProductController>()
.Run(c => c.Get("xyz"))
.AssertOK()
.AssertValue(new { id = "Xyz", description = "Xtra yellow elephant" });
}
[Test]
public void ServiceProvider()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys")).Request(HttpMethod.Get, "test").Respond.With("test output");
using var test = ApiTester.Create<Startup>();
var hc = test.ReplaceHttpClientFactory(mcf)
.Services.GetService<IHttpClientFactory>().CreateClient("XXX");
var r = hc.GetAsync("test").Result;
Assert.That(r, Is.Not.Null);
Assert.That(r.Content.ReadAsStringAsync().Result, Is.EqualTo("test output"));
}
[Test]
public void Configuration()
{
using var test = ApiTester.Create<Startup>();
var cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("VerySpecialValue"));
cv = test.Configuration.GetValue<string>("OtherKey");
Assert.That(cv, Is.EqualTo("OtherValue"));
}
[Test]
public void Configuration_Use()
{
using var test = ApiTester.Create<Startup>();
test.UseAdditionalConfiguration("SpecialKey", "NotSoSpecial");
var cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("NotSoSpecial"));
// Null to reset.
test.UseAdditionalConfiguration(null);
cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("VerySpecialValue"));
}
[Test]
public void DefaultHttpClient()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateDefaultClient(new Uri("https://someothersys"))
.Request(HttpMethod.Get, "products/default").Respond.WithJson(new { id = "Def", description = "Default" });
using var test = ApiTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.Controller<ProductController>()
.Run(c => c.Get())
.AssertOK()
.AssertValue(new { id = "Def", description = "Default" });
}
}
}