Skip to content

Commit 974c161

Browse files
committed
Use fluent assertions on exceptions
1 parent dd7341c commit 974c161

10 files changed

Lines changed: 84 additions & 52 deletions

File tree

YoutubeExplode.Converter.Tests/GeneralSpecs.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,19 @@ public async Task I_can_try_to_download_a_video_and_get_an_error_if_the_conversi
227227
using var dir = TempDir.Create();
228228
var filePath = Path.Combine(dir.Path, "video.mp4");
229229

230-
// Act & assert
231-
var ex = await Assert.ThrowsAnyAsync<Exception>(async () =>
230+
// Act
231+
var act = async () =>
232232
await youtube.Videos.DownloadAsync(
233233
"9bZkp7q19f0",
234234
filePath,
235235
o =>
236236
o.SetFFmpegPath(FFmpeg.FilePath)
237237
.SetContainer("invalid_format")
238238
.SetPreset(ConversionPreset.UltraFast)
239-
)
240-
);
239+
);
240+
241+
// Assert
242+
var ex = (await act.Should().ThrowAsync<Exception>()).Which;
241243

242244
Directory.EnumerateFiles(dir.Path, "*", SearchOption.AllDirectories).Should().BeEmpty();
243245

YoutubeExplode.Tests/ChannelHandleSpecs.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public void I_can_try_to_parse_a_channel_handle_and_get_an_error_if_the_input_st
4747
string channelHandleOrUrl
4848
)
4949
{
50-
// Act & assert
51-
Assert.Throws<ArgumentException>(() => ChannelHandle.Parse(channelHandleOrUrl));
50+
// Act
51+
var act = () => ChannelHandle.Parse(channelHandleOrUrl);
52+
53+
// Assert
54+
act.Should().ThrowExactly<ArgumentException>();
5255
}
5356
}

YoutubeExplode.Tests/ChannelIdSpecs.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public void I_can_try_to_parse_a_channel_ID_and_get_an_error_if_the_input_string
4646
string channelIdOrUrl
4747
)
4848
{
49-
// Act & assert
50-
Assert.Throws<ArgumentException>(() => ChannelId.Parse(channelIdOrUrl));
49+
// Act
50+
var act = () => ChannelId.Parse(channelIdOrUrl);
51+
52+
// Assert
53+
act.Should().ThrowExactly<ArgumentException>();
5154
}
5255
}

YoutubeExplode.Tests/ChannelSlugSpecs.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public void I_can_try_to_parse_a_channel_slug_and_get_an_error_if_the_input_stri
4848
string channelSlugOrUrl
4949
)
5050
{
51-
// Act & assert
52-
Assert.Throws<ArgumentException>(() => ChannelSlug.Parse(channelSlugOrUrl));
51+
// Act
52+
var act = () => ChannelSlug.Parse(channelSlugOrUrl);
53+
54+
// Assert
55+
act.Should().ThrowExactly<ArgumentException>();
5356
}
5457
}

YoutubeExplode.Tests/PlaylistIdSpecs.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ public void I_can_try_to_parse_a_playlist_ID_and_get_an_error_if_the_input_strin
7474
string playlistIdOrUrl
7575
)
7676
{
77-
// Act & assert
78-
Assert.Throws<ArgumentException>(() => PlaylistId.Parse(playlistIdOrUrl));
77+
// Act
78+
var act = () => PlaylistId.Parse(playlistIdOrUrl);
79+
80+
// Assert
81+
act.Should().ThrowExactly<ArgumentException>();
7982
}
8083
}

YoutubeExplode.Tests/PlaylistSpecs.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public async Task I_can_try_to_get_the_metadata_of_a_playlist_and_get_an_error_i
4141
// Arrange
4242
using var youtube = new YoutubeClient();
4343

44-
// Act & assert
45-
var ex = await Assert.ThrowsAsync<PlaylistUnavailableException>(async () =>
46-
await youtube.Playlists.GetAsync(PlaylistIds.Private)
47-
);
44+
// Act
45+
var act = async () => await youtube.Playlists.GetAsync(PlaylistIds.Private);
46+
47+
// Assert
48+
var ex = (await act.Should().ThrowAsync<PlaylistUnavailableException>()).Which;
4849

4950
testOutput.WriteLine(ex.ToString());
5051
}
@@ -55,10 +56,11 @@ public async Task I_can_try_to_get_the_metadata_of_a_playlist_and_get_an_error_i
5556
// Arrange
5657
using var youtube = new YoutubeClient();
5758

58-
// Act & assert
59-
var ex = await Assert.ThrowsAsync<PlaylistUnavailableException>(async () =>
60-
await youtube.Playlists.GetAsync(PlaylistIds.NonExisting)
61-
);
59+
// Act
60+
var act = async () => await youtube.Playlists.GetAsync(PlaylistIds.NonExisting);
61+
62+
// Assert
63+
var ex = (await act.Should().ThrowAsync<PlaylistUnavailableException>()).Which;
6264

6365
testOutput.WriteLine(ex.ToString());
6466
}

YoutubeExplode.Tests/StreamSpecs.cs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,12 @@ public async Task I_can_try_to_get_the_list_of_available_streams_of_a_video_and_
141141
// Arrange
142142
using var youtube = new YoutubeClient();
143143

144-
// Act & assert
145-
var ex = await Assert.ThrowsAsync<VideoRequiresPurchaseException>(async () =>
146-
await youtube.Videos.Streams.GetManifestAsync(VideoIds.RequiresPurchase)
147-
);
144+
// Act
145+
var act = async () =>
146+
await youtube.Videos.Streams.GetManifestAsync(VideoIds.RequiresPurchase);
147+
148+
// Assert
149+
var ex = (await act.Should().ThrowAsync<VideoRequiresPurchaseException>()).Which;
148150

149151
ex.PreviewVideoId.Value.Should().NotBeNullOrWhiteSpace();
150152

@@ -157,10 +159,11 @@ public async Task I_can_try_to_get_the_list_of_available_streams_of_a_video_and_
157159
// Arrange
158160
using var youtube = new YoutubeClient();
159161

160-
// Act & assert
161-
var ex = await Assert.ThrowsAsync<VideoUnavailableException>(async () =>
162-
await youtube.Videos.Streams.GetManifestAsync(VideoIds.Private)
163-
);
162+
// Act
163+
var act = async () => await youtube.Videos.Streams.GetManifestAsync(VideoIds.Private);
164+
165+
// Assert
166+
var ex = (await act.Should().ThrowAsync<VideoUnavailableException>()).Which;
164167

165168
testOutput.WriteLine(ex.ToString());
166169
}
@@ -171,10 +174,11 @@ public async Task I_can_try_to_get_the_list_of_available_streams_of_a_video_and_
171174
// Arrange
172175
using var youtube = new YoutubeClient();
173176

174-
// Act & assert
175-
var ex = await Assert.ThrowsAsync<VideoUnavailableException>(async () =>
176-
await youtube.Videos.Streams.GetManifestAsync(VideoIds.Deleted)
177-
);
177+
// Act
178+
var act = async () => await youtube.Videos.Streams.GetManifestAsync(VideoIds.Deleted);
179+
180+
// Assert
181+
var ex = (await act.Should().ThrowAsync<VideoUnavailableException>()).Which;
178182

179183
testOutput.WriteLine(ex.ToString());
180184
}
@@ -308,10 +312,12 @@ public async Task I_can_try_to_get_the_HTTP_live_stream_URL_for_a_video_and_get_
308312
// Arrange
309313
using var youtube = new YoutubeClient();
310314

311-
// Act & assert
312-
var ex = await Assert.ThrowsAsync<VideoUnplayableException>(async () =>
313-
await youtube.Videos.Streams.GetHttpLiveStreamUrlAsync(VideoIds.RequiresPurchase)
314-
);
315+
// Act
316+
var act = async () =>
317+
await youtube.Videos.Streams.GetHttpLiveStreamUrlAsync(VideoIds.RequiresPurchase);
318+
319+
// Assert
320+
var ex = (await act.Should().ThrowAsync<VideoUnplayableException>()).Which;
315321

316322
testOutput.WriteLine(ex.ToString());
317323
}
@@ -322,10 +328,12 @@ public async Task I_can_try_to_get_the_HTTP_live_stream_URL_for_a_video_and_get_
322328
// Arrange
323329
using var youtube = new YoutubeClient();
324330

325-
// Act & assert
326-
var ex = await Assert.ThrowsAsync<YoutubeExplodeException>(async () =>
327-
await youtube.Videos.Streams.GetHttpLiveStreamUrlAsync(VideoIds.Normal)
328-
);
331+
// Act
332+
var act = async () =>
333+
await youtube.Videos.Streams.GetHttpLiveStreamUrlAsync(VideoIds.Normal);
334+
335+
// Assert
336+
var ex = (await act.Should().ThrowAsync<YoutubeExplodeException>()).Which;
329337

330338
testOutput.WriteLine(ex.ToString());
331339
}

YoutubeExplode.Tests/UserNameSpecs.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public void I_can_try_to_parse_a_user_name_and_get_an_error_if_the_input_string_
4444
string userName
4545
)
4646
{
47-
// Act & assert
48-
Assert.Throws<ArgumentException>(() => UserName.Parse(userName));
47+
// Act
48+
var act = () => UserName.Parse(userName);
49+
50+
// Assert
51+
act.Should().ThrowExactly<ArgumentException>();
4952
}
5053
}

YoutubeExplode.Tests/VideoIdSpecs.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public void I_can_try_to_parse_a_video_ID_and_get_an_error_if_the_input_string_i
4848
string videoId
4949
)
5050
{
51-
// Act & assert
52-
Assert.Throws<ArgumentException>(() => VideoId.Parse(videoId));
51+
// Act
52+
var act = () => VideoId.Parse(videoId);
53+
54+
// Assert
55+
act.Should().ThrowExactly<ArgumentException>();
5356
}
5457
}

YoutubeExplode.Tests/VideoSpecs.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ public async Task I_can_try_to_get_the_metadata_of_a_video_and_get_an_error_if_i
6161
// Arrange
6262
using var youtube = new YoutubeClient();
6363

64-
// Act & assert
65-
var ex = await Assert.ThrowsAsync<VideoUnavailableException>(async () =>
66-
await youtube.Videos.GetAsync(VideoIds.Private)
67-
);
64+
// Act
65+
var act = async () => await youtube.Videos.GetAsync(VideoIds.Private);
66+
67+
// Assert
68+
var ex = (await act.Should().ThrowAsync<VideoUnavailableException>()).Which;
6869

6970
testOutput.WriteLine(ex.ToString());
7071
}
@@ -75,10 +76,11 @@ public async Task I_can_try_to_get_the_metadata_of_a_video_and_get_an_error_if_i
7576
// Arrange
7677
using var youtube = new YoutubeClient();
7778

78-
// Act & assert
79-
var ex = await Assert.ThrowsAsync<VideoUnavailableException>(async () =>
80-
await youtube.Videos.GetAsync(VideoIds.Deleted)
81-
);
79+
// Act
80+
var act = async () => await youtube.Videos.GetAsync(VideoIds.Deleted);
81+
82+
// Assert
83+
var ex = (await act.Should().ThrowAsync<VideoUnavailableException>()).Which;
8284

8385
testOutput.WriteLine(ex.ToString());
8486
}

0 commit comments

Comments
 (0)