forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceTypeRegistryTests.cs
More file actions
167 lines (134 loc) · 6.12 KB
/
Copy pathResourceTypeRegistryTests.cs
File metadata and controls
167 lines (134 loc) · 6.12 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using FluentAssertions;
using JSONAPI.Core;
using JSONAPI.Tests.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace JSONAPI.Tests.Core
{
[TestClass]
public class ResourceTypeRegistryTests
{
private class DerivedPost : Post
{
}
[TestMethod]
public void GetRegistrationForType_returns_correct_value_for_registered_types()
{
// Arrange
var mockPostRegistration = new Mock<IResourceTypeRegistration>(MockBehavior.Strict);
mockPostRegistration.Setup(m => m.Type).Returns(typeof(Post));
mockPostRegistration.Setup(m => m.ResourceTypeName).Returns("posts");
var mockAuthorRegistration = new Mock<IResourceTypeRegistration>(MockBehavior.Strict);
mockAuthorRegistration.Setup(m => m.Type).Returns(typeof(Author));
mockAuthorRegistration.Setup(m => m.ResourceTypeName).Returns("authors");
var registry = new ResourceTypeRegistry();
registry.AddRegistration(mockPostRegistration.Object);
registry.AddRegistration(mockAuthorRegistration.Object);
// Act
var authorReg = registry.GetRegistrationForType(typeof(Author));
var postReg = registry.GetRegistrationForType(typeof(Post));
// Assert
postReg.Should().BeSameAs(mockPostRegistration.Object);
authorReg.Should().BeSameAs(mockAuthorRegistration.Object);
}
[TestMethod]
public void GetRegistrationForType_gets_registration_for_closest_registered_base_type_for_unregistered_type()
{
// Arrange
var mockPostRegistration = new Mock<IResourceTypeRegistration>(MockBehavior.Strict);
mockPostRegistration.Setup(m => m.Type).Returns(typeof(Post));
mockPostRegistration.Setup(m => m.ResourceTypeName).Returns("posts");
var registry = new ResourceTypeRegistry();
registry.AddRegistration(mockPostRegistration.Object);
// Act
var registration = registry.GetRegistrationForType(typeof(DerivedPost));
// Assert
registration.Type.Should().Be(typeof(Post));
}
[TestMethod]
public void GetRegistrationForType_fails_when_getting_unregistered_type()
{
// Arrange
var registry = new ResourceTypeRegistry();
// Act
Action action = () =>
{
registry.GetRegistrationForType(typeof(Post));
};
// Assert
action.ShouldThrow<TypeRegistrationNotFoundException>().WithMessage("No type registration was found for the type \"Post\".");
}
[TestMethod]
public void GetRegistrationForResourceTypeName_fails_when_getting_unregistered_type_name()
{
// Arrange
var registry = new ResourceTypeRegistry();
// Act
Action action = () =>
{
registry.GetRegistrationForResourceTypeName("posts");
};
// Assert
action.ShouldThrow<TypeRegistrationNotFoundException>().WithMessage("No type registration was found for the type name \"posts\".");
}
[TestMethod]
public void GetRegistrationForResourceTypeName_returns_correct_value_for_registered_names()
{
// Arrange
var mockPostRegistration = new Mock<IResourceTypeRegistration>(MockBehavior.Strict);
mockPostRegistration.Setup(m => m.Type).Returns(typeof(Post));
mockPostRegistration.Setup(m => m.ResourceTypeName).Returns("posts");
var mockAuthorRegistration = new Mock<IResourceTypeRegistration>(MockBehavior.Strict);
mockAuthorRegistration.Setup(m => m.Type).Returns(typeof(Author));
mockAuthorRegistration.Setup(m => m.ResourceTypeName).Returns("authors");
var registry = new ResourceTypeRegistry();
registry.AddRegistration(mockPostRegistration.Object);
registry.AddRegistration(mockAuthorRegistration.Object);
// Act
var postReg = registry.GetRegistrationForResourceTypeName("posts");
var authorReg = registry.GetRegistrationForResourceTypeName("authors");
// Assert
postReg.Should().BeSameAs(mockPostRegistration.Object);
authorReg.Should().BeSameAs(mockAuthorRegistration.Object);
}
[TestMethod]
public void TypeIsRegistered_returns_true_if_type_is_registered()
{
// Arrange
var mockPostRegistration = new Mock<IResourceTypeRegistration>(MockBehavior.Strict);
mockPostRegistration.Setup(m => m.Type).Returns(typeof(Post));
mockPostRegistration.Setup(m => m.ResourceTypeName).Returns("posts");
var registry = new ResourceTypeRegistry();
registry.AddRegistration(mockPostRegistration.Object);
// Act
var isRegistered = registry.TypeIsRegistered(typeof(Post));
// Assert
isRegistered.Should().BeTrue();
}
[TestMethod]
public void TypeIsRegistered_returns_true_if_parent_type_is_registered()
{
// Arrange
var mockPostRegistration = new Mock<IResourceTypeRegistration>(MockBehavior.Strict);
mockPostRegistration.Setup(m => m.Type).Returns(typeof(Post));
mockPostRegistration.Setup(m => m.ResourceTypeName).Returns("posts");
var registry = new ResourceTypeRegistry();
registry.AddRegistration(mockPostRegistration.Object);
// Act
var isRegistered = registry.TypeIsRegistered(typeof(DerivedPost));
// Assert
isRegistered.Should().BeTrue();
}
[TestMethod]
public void TypeIsRegistered_returns_false_if_no_type_in_hierarchy_is_registered()
{
// Arrange
var registry = new ResourceTypeRegistry();
// Act
var isRegistered = registry.TypeIsRegistered(typeof(Comment));
// Assert
isRegistered.Should().BeFalse();
}
}
}