-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProductFunctionTest.cs
More file actions
69 lines (61 loc) · 2.7 KB
/
ProductFunctionTest.cs
File metadata and controls
69 lines (61 loc) · 2.7 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Net.Http;
using UnitTestEx.Function;
namespace UnitTestEx.MSTest.Test
{
[TestClass]
public class ProductFunctionTest
{
[TestMethod]
public void Notfound()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Get, "products/xyz").Respond.With(HttpStatusCode.NotFound);
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.HttpTrigger<ProductFunction>()
.Run(f => f.Run(test.CreateHttpRequest(HttpMethod.Get, "product/xyz"), "xyz", test.Logger))
.AssertNotFound();
}
[TestMethod]
public void Success()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Get, "products/abc").Respond.WithJson(new { id = "Abc", description = "A blue carrot" });
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.HttpTrigger<ProductFunction>()
.Run(f => f.Run(test.CreateHttpRequest(HttpMethod.Get, "product/abc"), "abc", test.Logger))
.AssertOK()
.AssertValue(new { id = "Abc", description = "A blue carrot" });
}
[TestMethod]
public void Success2()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Get, "products/abc").Respond.WithJson(new { id = "Abc", description = "A blue carrot" });
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.Type<ProductFunction>()
.Run(f => f.Run(test.CreateHttpRequest(HttpMethod.Get, "product/abc"), "abc", test.Logger))
.ToActionResultAssertor()
.AssertOK()
.AssertValue(new { id = "Abc", description = "A blue carrot" });
}
[TestMethod]
public void Exception()
{
var mcf = MockHttpClientFactory.Create();
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.HttpTrigger<ProductFunction>()
.Run(f => f.Run(test.CreateHttpRequest(HttpMethod.Get, "product/exception"), "exception", test.Logger))
.AssertException<InvalidOperationException>("An unexpected exception occured.");
}
}
}