This repository was archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAuthorServiceTests.Exceptions.GetAll.cs
89 lines (71 loc) · 3 KB
/
AuthorServiceTests.Exceptions.GetAll.cs
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
// ---------------------------------------------------------------
// Copyright (c) 2023 Planet Dotnet. All rights reserved.
// Licensed under the MIT License.
// See License.txt in the project root for license information.
// ---------------------------------------------------------------
using System;
using System.Threading.Tasks;
using Moq;
using PlanetDotnet.Authors.Models.Authors.Exceptions;
using Xunit;
namespace PlanetDotnet.Tests.Unit.Services.Foundations.Authors
{
public partial class AuthorServiceTests
{
[Theory]
[MemberData(nameof(DependencyExceptions))]
public async Task ShouldThrowCriticalDependencyExceptionOnRetrieveAllIfDependencyErrorOccursAndLogIt(
Exception dependencyException)
{
// given
var failedStorageException =
new FailedAuthorStorageException(dependencyException);
var expectedAuthorDependencyException =
new AuthorDependencyException(failedStorageException);
this.authorBrokerMock.Setup(broker =>
broker.GetAllAuthorsAsync())
.Throws(dependencyException);
// when
var retrieveAllAuthorsTask =
this.authorService.RetrieveAllAuthorsAsync();
// then
await Assert.ThrowsAsync<AuthorDependencyException>(() =>
retrieveAllAuthorsTask.AsTask());
this.authorBrokerMock.Verify(broker =>
broker.GetAllAuthorsAsync(),
Times.Once);
this.loggingBrokerMock.Verify(broker =>
broker.LogCritical(It.Is(SameExceptionAs(
expectedAuthorDependencyException))),
Times.Once);
this.authorBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
}
[Fact]
public async Task ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync()
{
// given
var serviceException = new Exception();
var expectedAuthorServiceException =
new AuthorServiceException(serviceException);
this.authorBrokerMock.Setup(broker =>
broker.GetAllAuthorsAsync())
.ThrowsAsync(serviceException);
// when
var retrievedAuthorTask =
this.authorService.RetrieveAllAuthorsAsync();
// then
await Assert.ThrowsAsync<AuthorServiceException>(() =>
retrievedAuthorTask.AsTask());
this.authorBrokerMock.Verify(broker =>
broker.GetAllAuthorsAsync(),
Times.Once);
this.loggingBrokerMock.Verify(broker =>
broker.LogError(It.Is(SameExceptionAs(
expectedAuthorServiceException))),
Times.Once);
this.authorBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
}
}
}