-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProductControllerTest.cs
More file actions
112 lines (87 loc) · 3.88 KB
/
ProductControllerTest.cs
File metadata and controls
112 lines (87 loc) · 3.88 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Net.Http;
using UnitTestEx.Api;
using UnitTestEx.Api.Controllers;
namespace UnitTestEx.MSTest.Test
{
[TestClass]
public class ProductControllerTest : WithApiTester<Startup>
{
[TestMethod]
public void Notfound_WithoutConfigurations()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Get, "products/xyz").Respond.With(HttpStatusCode.NotFound);
Startup.MessageProcessingHandler.WasExecuted = false;
Test.ReplaceHttpClientFactory(mcf)
.Controller<ProductController>()
.Run(c => c.Get("xyz"))
.AssertNotFound();
Assert.IsFalse(Startup.MessageProcessingHandler.WasExecuted);
}
[TestMethod]
public void Notfound_WithConfigurations()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX").WithConfigurations()
.Request(HttpMethod.Get, "products/xyz").Respond.With(HttpStatusCode.NotFound);
Startup.MessageProcessingHandler.WasExecuted = false;
Test.ReplaceHttpClientFactory(mcf)
.Controller<ProductController>()
.Run(c => c.Get("xyz"))
.AssertNotFound();
Assert.IsTrue(Startup.MessageProcessingHandler.WasExecuted);
}
[TestMethod]
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" });
}
[TestMethod]
public void 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 = hc.GetAsync("test").Result;
Assert.IsNotNull(r);
Assert.AreEqual("test output", r.Content.ReadAsStringAsync().Result);
}
[TestMethod]
public void MockHttpClientFactory_NoMocking()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX").WithoutMocking();
Startup.MessageProcessingHandler.WasExecuted = false;
var hc = Test.ReplaceHttpClientFactory(mcf)
.Services.GetService<IHttpClientFactory>().CreateClient("XXX");
var ex = Assert.ThrowsException<AggregateException>(() => hc.GetAsync("test").Result);
Assert.IsTrue(Startup.MessageProcessingHandler.WasExecuted);
mcf.VerifyAll();
}
[TestMethod]
public void MockHttpClientFactory_NoMocking_Exclude()
{
using var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX").WithoutMocking(typeof(Startup.MessageProcessingHandler));
Startup.MessageProcessingHandler.WasExecuted = false;
var hc = Test.ReplaceHttpClientFactory(mcf)
.Services.GetService<IHttpClientFactory>().CreateClient("XXX");
var ex = Assert.ThrowsException<AggregateException>(() => hc.GetAsync("test").Result);
Assert.IsFalse(Startup.MessageProcessingHandler.WasExecuted);
mcf.VerifyAll();
}
}
}