-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoomRepositoryTests.cs
179 lines (138 loc) · 5.38 KB
/
RoomRepositoryTests.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
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
168
169
170
171
172
173
174
175
176
177
178
179
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using ICT_LAB_Web.Components.Entities;
using ICT_LAB_Web.Components.Services.Interfaces;
using Moq;
using NUnit.Framework;
namespace ICT_LAB_Web.Tests
{
[TestFixture]
public class RoomRepositoryTests
{
private List<Room> TestData;
private Mock<IRoomRepository> RoomRepository;
#region Constructor
public RoomRepositoryTests()
{
this.InitializeData();
// Mock repo
RoomRepository = new Mock<IRoomRepository>();
// Get all rooms
RoomRepository.Setup(e => e.GetAll()).ReturnsAsync(TestData);
// Get room by room code
RoomRepository.Setup(e => e.Get(It.IsAny<string>())).ReturnsAsync((string room) => TestData.FirstOrDefault(q => q.RoomCode == room));
// Add room
RoomRepository.Setup(e => e.Add(It.IsAny<Room>())).ReturnsAsync((Room room) => { TestData.Add(room); TestData.FirstOrDefault(q => q.RoomCode == room.RoomCode); return room; });
// Update room
RoomRepository.Setup(e => e.Update(It.IsAny<Room>())).ReturnsAsync((Room room) => { var original = TestData.FirstOrDefault(q => q.RoomCode == room.RoomCode); original = room; return room; });
// Delete room
RoomRepository.Setup(e => e.Delete(It.IsAny<string>())).ReturnsAsync((string room) => { TestData.Remove(TestData.FirstOrDefault(q => q.RoomCode == room)); return TestData.FirstOrDefault(q => q.RoomCode == room) == null ? true : false; });
}
#endregion
#region Tests
[Test]
[Order(1)]
public async Task Get_All()
{
// Arrange
var repo = RoomRepository.Object;
// Act
var result = await repo.GetAll();
// Assert
var rooms = result.Should().BeAssignableTo<List<Room>>().Subject;
rooms.Should().NotBeNull();
rooms.Count().Should().Be(5);
}
[Test]
[Order(2)]
public async Task Get_By_RoomCode()
{
// Arrange
var repo = RoomRepository.Object;
var roomCode = "H.5.314";
// Act
var result = await repo.Get(roomCode);
// Assert
var room = result.Should().BeOfType<Room>().Subject;
room.Should().NotBeNull();
room.RoomCode.Should().Be("H.5.314");
room.StudentCapacity.Should().Be(25);
}
[Test]
[Order(3)]
public async Task Add()
{
// Arrange
var repo = RoomRepository.Object;
var newRoom = new Room {
RoomCode = "WD.01.016",
StudentCapacity = 35,
HasComputer = true,
HasSmartboard = true,
HasWindows = true
};
// Act
var result = await repo.Add(newRoom);
// Assert
var room = result.Should().BeOfType<Room>().Subject;
room.Should().NotBeNull();
room.RoomCode.Should().Be("WD.01.016");
room.StudentCapacity.Should().Be(35);
TestData.Count.Should().Be(6);
}
[Test]
[Order(4)]
public async Task Update()
{
// Arrange
var repo = RoomRepository.Object;
var originalRoom = TestData.FirstOrDefault(q => q.RoomCode == "WD.01.016");
var roomToUpdate = new Room
{
RoomCode = "WD.01.016",
StudentCapacity = 30,
HasComputer = true,
HasSmartboard = true,
HasWindows = true
};
// Act
var result = await repo.Update(roomToUpdate);
// Assert
var room = result.Should().BeOfType<Room>().Subject;
room.Should().NotBeNull();
room.RoomCode.Should().Be("WD.01.016");
room.StudentCapacity.Should().Be(30);
room.Should().NotBeSameAs(originalRoom);
}
[Test]
[Order(5)]
public async Task Delete()
{
// Arrange
var repo = RoomRepository.Object;
var roomToDelete = TestData[0];
// Act
var result = await repo.Delete(roomToDelete.RoomCode);
// Assert
result.Should().BeTrue();
TestData.Count.Should().Be(5);
}
#endregion
#region Setup Data
private void InitializeData()
{
var rooms = new List<Room>
{
new Room{ RoomCode = "H.4.308", HasComputer = false, HasSmartboard = false, HasWindows = true, StudentCapacity = 25 },
new Room{ RoomCode = "H.1.306", HasComputer = true, HasSmartboard = true, HasWindows = false, StudentCapacity = 30 },
new Room{ RoomCode = "H.5.314", HasComputer = true, HasSmartboard = true, HasWindows = true, StudentCapacity = 25 },
new Room{ RoomCode = "WD.04.204", HasComputer = true, HasSmartboard = true, HasWindows = false, StudentCapacity = 25 },
new Room{ RoomCode = "WD.04.002", HasComputer = true, HasSmartboard = false, HasWindows = true, StudentCapacity = 35 },
};
this.TestData = rooms;
}
#endregion
}
}