-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPersonControllerTest.cs
More file actions
108 lines (97 loc) · 3.49 KB
/
PersonControllerTest.cs
File metadata and controls
108 lines (97 loc) · 3.49 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
using System.Collections.Generic;
using System.Threading.Tasks;
using UnitTestEx.Api;
using UnitTestEx.Api.Controllers;
using UnitTestEx.Api.Models;
using Xunit;
using Xunit.Abstractions;
namespace UnitTestEx.Xunit.Test
{
public class PersonControllerTest : UnitTestBase
{
public PersonControllerTest(ITestOutputHelper output) : base(output) { }
[Fact]
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" });
}
[Fact]
public void Get_Test2()
{
int id = 2;
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.Run(c => c.Get(id))
.AssertOK()
.AssertValue(new Person { Id = id, FirstName = "Jane", LastName = "Jones" });
}
[Fact]
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);
}
[Fact]
public void Get_Test4()
{
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.Run(c => c.Get(4))
.AssertNotFound();
}
[Fact]
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");
}
[Fact]
public void GetByArgs_Test2()
{
using var test = ApiTester.Create<Startup>();
test.Controller<PersonController>()
.Run(c => c.GetByArgs(null, null, null))
.AssertOK()
.AssertValue("--");
}
[Fact]
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" });
}
[Fact]
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.");
}
[Fact]
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."));
}
}
}