-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStudentServiceTests.Exceptions.RetrieveAll.cs
More file actions
83 lines (64 loc) · 2.96 KB
/
StudentServiceTests.Exceptions.RetrieveAll.cs
File metadata and controls
83 lines (64 loc) · 2.96 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
// -----------------------------------------------------------------------
// Copyright (c) Signature Chess Club & MumsWhoCode. All rights reserved.
// -----------------------------------------------------------------------
using Microsoft.Data.SqlClient;
using Moq;
using SCMS.Services.Api.Models.Foundations.Students.Exceptions;
using System;
using Xunit;
namespace SCMS.Services.Tests.Unit.Services.Foundations.Students
{
public partial class StudentServiceTests
{
[Fact]
public void ShouldThrowCriticalDependencyExceptionOnRetrieveAllIfSqlErrorOccursAndLogItAsync()
{
//given
SqlException sqlException = GetSqlException();
var failedStudentStorageException =
new FailedStudentStorageException(sqlException);
var expectedStudentDependencyException =
new StudentDependencyException(failedStudentStorageException);
this.storageBrokerMock.Setup(broker =>
broker.SelectAllStudents()).Throws(sqlException);
//when
Action retrieveAllStudentsAction = () =>
this.studentService.RetrieveAllStudents();
//then
Assert.Throws<StudentDependencyException>(retrieveAllStudentsAction);
this.storageBrokerMock.Verify(broker =>
broker.SelectAllStudents(), Times.Once);
this.loggingBrokerMock.Verify(broker =>
broker.LogCritical(It.Is(SameExceptionAs(
expectedStudentDependencyException))), Times.Once);
this.storageBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
this.dateTimeBrokerMock.VerifyNoOtherCalls();
}
[Fact]
public void ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync()
{
//given
var seviceException = new Exception();
var failedStudentServiceException =
new FailedStudentServiceException(seviceException);
var expectedStudentServiceException =
new StudentServiceException(failedStudentServiceException);
this.storageBrokerMock.Setup(broker =>
broker.SelectAllStudents()).Throws(seviceException);
//when
Action retrieveAllStudentsAction = () =>
this.studentService.RetrieveAllStudents();
//then
Assert.Throws<StudentServiceException>(retrieveAllStudentsAction);
this.storageBrokerMock.Verify(broker =>
broker.SelectAllStudents(), Times.Once);
this.loggingBrokerMock.Verify(broker =>
broker.LogError(It.Is(SameExceptionAs(
expectedStudentServiceException))), Times.Once);
this.storageBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
this.dateTimeBrokerMock.VerifyNoOtherCalls();
}
}
}