Skip to content

Commit 55534af

Browse files
author
Bart Koelman
committed
Split up Act and Assert steps in tests
1 parent 8d887e2 commit 55534af

File tree

8 files changed

+117
-70
lines changed

8 files changed

+117
-70
lines changed

test/UnitTests/Controllers/BaseJsonApiControllerTests.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Net;
23
using System.Net.Http;
34
using System.Threading;
@@ -41,9 +42,10 @@ public async Task GetAsync_Throws_405_If_No_Service()
4142
var controller = new ResourceController(new Mock<IJsonApiOptions>().Object, NullLoggerFactory.Instance, null);
4243

4344
// Act
44-
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(() => controller.GetAsync(CancellationToken.None));
45+
Func<Task> asyncAction = () => controller.GetAsync(CancellationToken.None);
4546

4647
// Assert
48+
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(asyncAction);
4749
Assert.Equal(HttpStatusCode.MethodNotAllowed, exception.Errors[0].StatusCode);
4850
Assert.Equal(HttpMethod.Get, exception.Method);
4951
}
@@ -71,9 +73,10 @@ public async Task GetAsyncById_Throws_405_If_No_Service()
7173
var controller = new ResourceController(new Mock<IJsonApiOptions>().Object, NullLoggerFactory.Instance);
7274

7375
// Act
74-
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(() => controller.GetAsync(id, CancellationToken.None));
76+
Func<Task> asyncAction = () => controller.GetAsync(id, CancellationToken.None);
7577

7678
// Assert
79+
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(asyncAction);
7780
Assert.Equal(HttpStatusCode.MethodNotAllowed, exception.Errors[0].StatusCode);
7881
Assert.Equal(HttpMethod.Get, exception.Method);
7982
}
@@ -102,10 +105,10 @@ public async Task GetRelationshipsAsync_Throws_405_If_No_Service()
102105
var controller = new ResourceController(new Mock<IJsonApiOptions>().Object, NullLoggerFactory.Instance);
103106

104107
// Act
105-
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(() =>
106-
controller.GetRelationshipAsync(id, "articles", CancellationToken.None));
108+
Func<Task> asyncAction = () => controller.GetRelationshipAsync(id, "articles", CancellationToken.None);
107109

108110
// Assert
111+
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(asyncAction);
109112
Assert.Equal(HttpStatusCode.MethodNotAllowed, exception.Errors[0].StatusCode);
110113
Assert.Equal(HttpMethod.Get, exception.Method);
111114
}
@@ -134,10 +137,10 @@ public async Task GetRelationshipAsync_Throws_405_If_No_Service()
134137
var controller = new ResourceController(new Mock<IJsonApiOptions>().Object, NullLoggerFactory.Instance);
135138

136139
// Act
137-
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(() =>
138-
controller.GetSecondaryAsync(id, "articles", CancellationToken.None));
140+
Func<Task> asyncAction = () => controller.GetSecondaryAsync(id, "articles", CancellationToken.None);
139141

140142
// Assert
143+
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(asyncAction);
141144
Assert.Equal(HttpStatusCode.MethodNotAllowed, exception.Errors[0].StatusCode);
142145
Assert.Equal(HttpMethod.Get, exception.Method);
143146
}
@@ -168,9 +171,10 @@ public async Task PatchAsync_Throws_405_If_No_Service()
168171
var controller = new ResourceController(new Mock<IJsonApiOptions>().Object, NullLoggerFactory.Instance);
169172

170173
// Act
171-
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(() => controller.PatchAsync(id, resource, CancellationToken.None));
174+
Func<Task> asyncAction = () => controller.PatchAsync(id, resource, CancellationToken.None);
172175

173176
// Assert
177+
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(asyncAction);
174178
Assert.Equal(HttpStatusCode.MethodNotAllowed, exception.Errors[0].StatusCode);
175179
Assert.Equal(HttpMethod.Patch, exception.Method);
176180
}
@@ -221,10 +225,10 @@ public async Task PatchRelationshipsAsync_Throws_405_If_No_Service()
221225
var controller = new ResourceController(new Mock<IJsonApiOptions>().Object, NullLoggerFactory.Instance);
222226

223227
// Act
224-
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(() =>
225-
controller.PatchRelationshipAsync(id, "articles", null, CancellationToken.None));
228+
Func<Task> asyncAction = () => controller.PatchRelationshipAsync(id, "articles", null, CancellationToken.None);
226229

227230
// Assert
231+
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(asyncAction);
228232
Assert.Equal(HttpStatusCode.MethodNotAllowed, exception.Errors[0].StatusCode);
229233
Assert.Equal(HttpMethod.Patch, exception.Method);
230234
}
@@ -252,9 +256,10 @@ public async Task DeleteAsync_Throws_405_If_No_Service()
252256
var controller = new ResourceController(new Mock<IJsonApiOptions>().Object, NullLoggerFactory.Instance);
253257

254258
// Act
255-
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(() => controller.DeleteAsync(id, CancellationToken.None));
259+
Func<Task> asyncAction = () => controller.DeleteAsync(id, CancellationToken.None);
256260

257261
// Assert
262+
var exception = await Assert.ThrowsAsync<RequestMethodNotAllowedException>(asyncAction);
258263
Assert.Equal(HttpStatusCode.MethodNotAllowed, exception.Errors[0].StatusCode);
259264
Assert.Equal(HttpMethod.Delete, exception.Method);
260265
}

test/UnitTests/Extensions/ServiceCollectionExtensionsTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,11 @@ public void AddResourceService_Throws_If_Type_Does_Not_Implement_Any_Interfaces(
131131
// Arrange
132132
var services = new ServiceCollection();
133133

134-
// Act, assert
135-
Assert.Throws<InvalidConfigurationException>(() => services.AddResourceService<int>());
134+
// Act
135+
Action action = () => services.AddResourceService<int>();
136+
137+
// Assert
138+
Assert.Throws<InvalidConfigurationException>(action);
136139
}
137140

138141
[Fact]

test/UnitTests/Internal/TypeHelperTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public void Bad_DateTimeOffset_String_Throws()
2929
const string formattedString = "this_is_not_a_valid_dto";
3030

3131
// Act
32+
Action action = () => TypeHelper.ConvertType(formattedString, typeof(DateTimeOffset));
33+
3234
// Assert
33-
Assert.Throws<FormatException>(() => TypeHelper.ConvertType(formattedString, typeof(DateTimeOffset)));
35+
Assert.Throws<FormatException>(action);
3436
}
3537

3638
[Fact]
@@ -82,7 +84,7 @@ public void ConvertType_Returns_Value_If_Type_Is_Assignable()
8284
[Fact]
8385
public void ConvertType_Returns_Default_Value_For_Empty_Strings()
8486
{
85-
// Arrange -- can't use non-constants in [Theory]
87+
// Arrange
8688
var data = new Dictionary<Type, object>
8789
{
8890
{ typeof(int), 0 },
@@ -105,14 +107,14 @@ public void ConvertType_Returns_Default_Value_For_Empty_Strings()
105107
[Fact]
106108
public void Can_Convert_TimeSpans()
107109
{
108-
//arrange
110+
// Arrange
109111
TimeSpan timeSpan = TimeSpan.FromMinutes(45);
110112
string stringSpan = timeSpan.ToString();
111113

112-
//act
114+
// Act
113115
object result = TypeHelper.ConvertType(stringSpan, typeof(TimeSpan));
114116

115-
//assert
117+
// Assert
116118
Assert.Equal(timeSpan, result);
117119
}
118120

@@ -122,8 +124,11 @@ public void Bad_TimeSpanString_Throws()
122124
// Arrange
123125
const string formattedString = "this_is_not_a_valid_timespan";
124126

125-
// Act/assert
126-
Assert.Throws<FormatException>(() => TypeHelper.ConvertType(formattedString, typeof(TimeSpan)));
127+
// Act
128+
Action action = () => TypeHelper.ConvertType(formattedString, typeof(TimeSpan));
129+
130+
// Assert
131+
Assert.Throws<FormatException>(action);
127132
}
128133

129134
[Fact]

test/UnitTests/Middleware/JsonApiMiddlewareTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ public async Task ParseUrlBase_UrlHasNegativePrimaryIdAndTypeIsInt_ShouldNotThro
6565
// Arrange
6666
InvokeConfiguration configuration = GetConfiguration("/users/-5/");
6767

68-
// Act / Assert
69-
await RunMiddlewareTask(configuration);
68+
// Act
69+
Func<Task> asyncAction = async () => await RunMiddlewareTask(configuration);
70+
71+
// Assert
72+
await asyncAction();
7073
}
7174

7275
private Task RunMiddlewareTask(InvokeConfiguration holder)

test/UnitTests/ResourceHooks/Executor/Read/BeforeReadTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public void BeforeRead()
2626

2727
// Act
2828
hookExecutor.BeforeRead<TodoItem>(ResourcePipeline.Get);
29+
2930
// Assert
3031
todoResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, false, null), Times.Once());
3132
VerifyNoOtherCalls(todoResourceMock);
@@ -48,6 +49,7 @@ public void BeforeReadWithInclusion()
4849

4950
// Act
5051
hookExecutor.BeforeRead<TodoItem>(ResourcePipeline.Get);
52+
5153
// Assert
5254
todoResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, false, null), Times.Once());
5355
ownerResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, true, null), Times.Once());
@@ -72,6 +74,7 @@ public void BeforeReadWithNestedInclusion()
7274

7375
// Act
7476
hookExecutor.BeforeRead<TodoItem>(ResourcePipeline.Get);
77+
7578
// Assert
7679
todoResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, false, null), Times.Once());
7780
ownerResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, true, null), Times.Once());
@@ -97,6 +100,7 @@ public void BeforeReadWithNestedInclusion_No_Parent_Hook_Implemented()
97100

98101
// Act
99102
hookExecutor.BeforeRead<TodoItem>(ResourcePipeline.Get);
103+
100104
// Assert
101105
ownerResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, true, null), Times.Once());
102106
passportResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, true, null), Times.Once());
@@ -121,6 +125,7 @@ public void BeforeReadWithNestedInclusion_No_Child_Hook_Implemented()
121125

122126
// Act
123127
hookExecutor.BeforeRead<TodoItem>(ResourcePipeline.Get);
128+
124129
// Assert
125130
todoResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, false, null), Times.Once());
126131
passportResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, true, null), Times.Once());
@@ -145,6 +150,7 @@ public void BeforeReadWithNestedInclusion_No_Grandchild_Hook_Implemented()
145150

146151
// Act
147152
hookExecutor.BeforeRead<TodoItem>(ResourcePipeline.Get);
153+
148154
// Assert
149155
todoResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, false, null), Times.Once());
150156
ownerResourceMock.Verify(rd => rd.BeforeRead(ResourcePipeline.Get, true, null), Times.Once());
@@ -169,6 +175,7 @@ public void BeforeReadWithNestedInclusion_Without_Any_Hook_Implemented()
169175

170176
// Act
171177
hookExecutor.BeforeRead<TodoItem>(ResourcePipeline.Get);
178+
172179
// Assert
173180
VerifyNoOtherCalls(todoResourceMock, ownerResourceMock, passportResourceMock);
174181
}

test/UnitTests/ResourceHooks/RelationshipDictionaryTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,11 @@ public void ResourceDiff_Loops_Over_Diffs()
220220

221221
var diffs = new DiffableResourceHashSet<Dummy>(_allResources, dbResources, _relationships, null);
222222

223-
// Assert & act
224-
foreach (ResourceDiffPair<Dummy> diff in diffs.GetDiffs())
223+
// Act
224+
ResourceDiffPair<Dummy>[] resourceDiffPairs = diffs.GetDiffs().ToArray();
225+
226+
// Assert
227+
foreach (ResourceDiffPair<Dummy> diff in resourceDiffPairs)
225228
{
226229
Assert.Equal(diff.Resource.Id, diff.DatabaseValue.Id);
227230
Assert.NotEqual(diff.Resource, diff.DatabaseValue);

test/UnitTests/Serialization/Common/BaseDocumentParserTests.cs

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -130,43 +130,45 @@ public void DeserializeAttributes_VariousDataTypes_CanDeserialize(string member,
130130

131131
string body = JsonConvert.SerializeObject(content);
132132

133-
// Act, assert
134-
if (expectError)
135-
{
136-
Assert.ThrowsAny<FormatException>(() => _deserializer.Deserialize(body));
137-
return;
138-
}
139-
140133
// Act
141-
var resource = (TestResource)_deserializer.Deserialize(body);
134+
Func<TestResource> action = () => (TestResource)_deserializer.Deserialize(body);
142135

143136
// Assert
144-
PropertyInfo pi = ResourceGraph.GetResourceContext("testResource").Attributes.Single(attr => attr.PublicName == member).Property;
145-
object deserializedValue = pi.GetValue(resource);
146-
147-
if (member == "intField")
148-
{
149-
Assert.Equal(1, deserializedValue);
150-
}
151-
else if (member == "nullableIntField" && value == null)
152-
{
153-
Assert.Null(deserializedValue);
154-
}
155-
else if (member == "nullableIntField" && (string)value == "1")
156-
{
157-
Assert.Equal(1, deserializedValue);
158-
}
159-
else if (member == "guidField")
160-
{
161-
Assert.Equal(deserializedValue, Guid.Parse("1a68be43-cc84-4924-a421-7f4d614b7781"));
162-
}
163-
else if (member == "dateTimeField")
137+
if (expectError)
164138
{
165-
Assert.Equal(deserializedValue, DateTime.Parse("9/11/2019 11:41:40 AM"));
139+
Assert.ThrowsAny<FormatException>(action);
166140
}
167141
else
168142
{
169-
Assert.Equal(value, deserializedValue);
143+
TestResource resource = action();
144+
145+
PropertyInfo pi = ResourceGraph.GetResourceContext("testResource").Attributes.Single(attr => attr.PublicName == member).Property;
146+
object deserializedValue = pi.GetValue(resource);
147+
148+
if (member == "intField")
149+
{
150+
Assert.Equal(1, deserializedValue);
151+
}
152+
else if (member == "nullableIntField" && value == null)
153+
{
154+
Assert.Null(deserializedValue);
155+
}
156+
else if (member == "nullableIntField" && (string)value == "1")
157+
{
158+
Assert.Equal(1, deserializedValue);
159+
}
160+
else if (member == "guidField")
161+
{
162+
Assert.Equal(deserializedValue, Guid.Parse("1a68be43-cc84-4924-a421-7f4d614b7781"));
163+
}
164+
else if (member == "dateTimeField")
165+
{
166+
Assert.Equal(deserializedValue, DateTime.Parse("9/11/2019 11:41:40 AM"));
167+
}
168+
else
169+
{
170+
Assert.Equal(value, deserializedValue);
171+
}
170172
}
171173
}
172174

@@ -251,8 +253,11 @@ public void DeserializeRelationship_SingleDataForToOneRelationship_CannotDeseria
251253

252254
string body = JsonConvert.SerializeObject(content);
253255

254-
// Act, assert
255-
Assert.Throws<JsonApiSerializationException>(() => _deserializer.Deserialize(body));
256+
// Act
257+
Action action = () => _deserializer.Deserialize(body);
258+
259+
// Assert
260+
Assert.Throws<JsonApiSerializationException>(action);
256261
}
257262

258263
[Fact]
@@ -275,8 +280,11 @@ public void DeserializeRelationship_ManyDataForToManyRelationship_CannotDeserial
275280

276281
string body = JsonConvert.SerializeObject(content);
277282

278-
// Act, assert
279-
Assert.Throws<JsonApiSerializationException>(() => _deserializer.Deserialize(body));
283+
// Act
284+
Action action = () => _deserializer.Deserialize(body);
285+
286+
// Assert
287+
Assert.Throws<JsonApiSerializationException>(action);
280288
}
281289

282290
[Fact]
@@ -336,7 +344,7 @@ public void DeserializeRelationships_EmptyRequiredOneToOnePrincipal_NavigationIs
336344
// Act
337345
var result = (OneToOneRequiredDependent)_deserializer.Deserialize(body);
338346

339-
// assert
347+
// Assert
340348
Assert.Equal(1, result.Id);
341349
Assert.Null(result.Principal);
342350
}
@@ -384,7 +392,7 @@ public void DeserializeRelationships_EmptyOneToManyRequiredPrincipal_NavigationI
384392
// Act
385393
var result = (OneToManyRequiredDependent)_deserializer.Deserialize(body);
386394

387-
// assert
395+
// Assert
388396
Assert.Equal(1, result.Id);
389397
Assert.Null(result.Principal);
390398
Assert.Null(result.AttributeMember);

0 commit comments

Comments
 (0)