-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPersonControllerTest.cs
More file actions
153 lines (139 loc) · 5.26 KB
/
PersonControllerTest.cs
File metadata and controls
153 lines (139 loc) · 5.26 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Net.Mime;
using System.Threading.Tasks;
using UnitTestEx.Api;
using UnitTestEx.Api.Controllers;
using UnitTestEx.Api.Models;
using UnitTestEx.Expectations;
namespace UnitTestEx.MSTest.Test
{
[TestClass]
public class PersonControllerTest
{
[TestMethod]
public void VerifyNewtonsoftJsonSerializer()
{
using var test = ApiTester.Create<Startup>();
Assert.IsInstanceOfType(TestSetUp.Default.JsonSerializer, typeof(NewtonsoftJsonSerializer));
}
[TestMethod]
public async Task Get_Test1()
{
using var test = ApiTester.Create<Startup>();
(await test.Controller<PersonController>()
.RunAsync(c => c.Get(1)))
.AssertOK()
.AssertValue(new Person { Id = 1, FirstName = "Bob", LastName = "Smith" });
}
[TestMethod]
public void Get_Test2()
{
int id = 2;
using var test = ApiTester.Create<Startup>().UseJsonSerializer(new NewtonsoftJsonSerializer());
test.Controller<PersonController>()
.Run(c => c.Get(id))
.AssertOK()
.AssertValue(new Person { Id = id, FirstName = "Jane", LastName = "Jones" });
}
[TestMethod]
public void Get_Test3()
{
var p = new Person { Id = 3, FirstName = "Brad", LastName = "Davies" };
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>().Run(c => c.Get(p.Id)).AssertOK().AssertValue(p);
}
[TestMethod]
public void Get_Test4()
{
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.Run(c => c.Get(4))
.AssertNotFound();
}
[TestMethod]
public void GetByArgs_Test1()
{
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.Run(c => c.GetByArgs("Mary", "Brown", new List<int> { 88, 99 }))
.AssertOK()
.AssertValue("Mary-Brown-88,99");
}
[TestMethod]
public void GetByArgs_Test2()
{
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.Run(c => c.GetByArgs(null, null, null))
.AssertOK()
.AssertValue("--");
}
[TestMethod]
public void Update_Test1()
{
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.Run(c => c.Update(1, new Person { FirstName = "Bob", LastName = "Smith" }))
.AssertOK()
.AssertValue(new Person { Id = 1, FirstName = "Bob", LastName = "Smith" });
}
[TestMethod]
public void Update_Test2()
{
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.Run(c => c.Update(1, new Person { FirstName = null, LastName = null }))
.AssertBadRequest()
.AssertErrors(
"First name is required.",
"Last name is required.");
}
[TestMethod]
public void Update_Test3()
{
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.Run(c => c.Update(1, new Person { FirstName = null, LastName = null }))
.AssertBadRequest()
.AssertErrors(
new ApiError("firstName", "First name is required."),
new ApiError("lastName", "Last name is required."));
}
[TestMethod]
public void Update_Test4()
{
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.Run(c => c.Update(1, null), new Person { FirstName = null, LastName = null })
.AssertBadRequest()
.AssertErrors(
new ApiError("firstName", "First name is required."),
new ApiError("lastName", "Last name is required."));
}
[TestMethod]
public void Update_Test5()
{
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.RunContent(c => c.Update(1, null), "{\"firstName\":null,\"lastName\":null}", MediaTypeNames.Application.Json)
.AssertBadRequest()
.AssertErrors(
new ApiError("firstName", "First name is required."),
new ApiError("lastName", "Last name is required."));
}
[TestMethod]
public void TypeTester()
{
using var test = ApiTester.Create<Startup>();
test.Type<PersonController>()
.ExpectSuccess()
.Run(c => c.Get(1))
.ToActionResultAssertor()
.AssertOK()
.AssertValue(new Person { Id = 1, FirstName = "Bob", LastName = "Smith" });
}
}
}