Skip to content

Commit 5185bb7

Browse files
Added xUnit and tests for 2 GET methods
1 parent 61aac3e commit 5185bb7

File tree

3 files changed

+195
-2
lines changed

3 files changed

+195
-2
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using AutoMapper;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.Logging;
4+
using Moq;
5+
using Server.Controllers;
6+
using Server.DTOs;
7+
using Server.Interfaces;
8+
using Server.Models;
9+
using Server.Profiles;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using Xunit;
13+
14+
namespace Server.Tests
15+
{
16+
public class InformationCardsControllerTests
17+
{
18+
[Fact]
19+
public void GetInformationCards_WithAListOfCards_ReturnAListOfCards()
20+
{
21+
// Arrange
22+
23+
var repository = GenerateMockRepository();
24+
25+
repository.Setup(repo => repo.GetAllInformationCards()).Returns(GetTestCards());
26+
27+
var mapper = GenerateMockMapper();
28+
var controller = GenerateMockController(repository, mapper);
29+
30+
// Act
31+
var result = controller.GetInformationCards();
32+
33+
// Assert
34+
35+
// 1. Checking output result for Not Null
36+
Assert.NotNull(result);
37+
38+
// 2. Checking output result for output object type as ActionResult<IEnumerable<InformationCardReadDto>>
39+
var actionResult = Assert.IsType<ActionResult<IEnumerable<InformationCardReadDto>>>(result);
40+
41+
// 3. Checking output result for object type as OkObjectResult (200 OK)
42+
var model = Assert.IsAssignableFrom<OkObjectResult>(actionResult.Result);
43+
44+
// 4. Checking if count of the returned objects equal to the real objects count
45+
var modelAsAList = model.Value as List<InformationCardReadDto>;
46+
Assert.Equal(GetTestCards().Count, modelAsAList.Count);
47+
}
48+
49+
[Fact]
50+
public void GetInformationCardById_WithAnExistingId_ReturnItemWithPassedIndex()
51+
{
52+
// Arrange
53+
var repository = GenerateMockRepository();
54+
repository.Setup(repo => repo.GetInformationCardById(1)).Returns(GetTestCards().FirstOrDefault(e => e.Id == 1));
55+
56+
var mapper = GenerateMockMapper();
57+
var controller = GenerateMockController(repository, mapper);
58+
59+
// Act
60+
var result = controller.GetInformationCardById(1);
61+
62+
// Assert
63+
64+
// 1. Checking output result for object type as OkObjectResult (200 OK)
65+
var actionResult = Assert.IsAssignableFrom<OkObjectResult>(result.Result);
66+
67+
// 2. Checking if data is right
68+
var model = Assert.IsType<InformationCardReadDto>(actionResult.Value);
69+
Assert.Equal(1, model.Id);
70+
Assert.Equal("Dog", model.Name);
71+
Assert.Equal("../../../Data/Images/Dog.jpg", model.Image);
72+
}
73+
74+
[Fact]
75+
public void GetInformationCardById_WithNotExistingId_ReturnNotFound()
76+
{
77+
// Arrange
78+
var repository = GenerateMockRepository();
79+
repository.Setup(repo => repo.GetInformationCardById(10)).Returns(GetTestCards().FirstOrDefault(e => e.Id == 10));
80+
81+
var mapper = GenerateMockMapper();
82+
var controller = GenerateMockController(repository, mapper);
83+
84+
// Act
85+
var result = controller.GetInformationCardById(10);
86+
87+
// Assert
88+
89+
// 1. Checking output result for object type as NotFoundObjectResult (404 Not Found)
90+
var actionResult = Assert.IsAssignableFrom<NotFoundObjectResult>(result.Result);
91+
92+
// 2. Checking if data is right
93+
var model = Assert.IsType<InformationCardReadDto>(actionResult.Value);
94+
Assert.Equal(null, model);
95+
}
96+
97+
98+
99+
100+
101+
102+
private List<InformationCard> GetTestCards()
103+
{
104+
var cards = new List<InformationCard>()
105+
{
106+
new InformationCard
107+
{
108+
Id = 0,
109+
Name = "Cat",
110+
Image = "../../../Data/Images/Cat.png"
111+
},
112+
new InformationCard
113+
{
114+
Id = 1,
115+
Name = "Dog",
116+
Image = "../../../Data/Images/Dog.jpg"
117+
},
118+
new InformationCard
119+
{
120+
Id = 2,
121+
Name = "Shark",
122+
Image = "../../../Data/Images/Shark.png"
123+
},
124+
new InformationCard
125+
{
126+
Id = 3,
127+
Name = "Ant",
128+
Image = "../../../Data/Images/Ant.jpg"
129+
},
130+
new InformationCard
131+
{
132+
Id = 4,
133+
Name = "Squirrel",
134+
Image = "../../../Data/Images/Squirrel.png"
135+
}
136+
};
137+
138+
return cards;
139+
}
140+
141+
private Mock<IInformationCardRepo> GenerateMockRepository()
142+
{
143+
return new Mock<IInformationCardRepo>();
144+
}
145+
private IMapper GenerateMockMapper()
146+
{
147+
var mockMapper = new MapperConfiguration(config =>
148+
{
149+
config.AddProfile(new InformationCardProfile());
150+
});
151+
152+
return mockMapper.CreateMapper();
153+
}
154+
private InformationCardsController GenerateMockController(Mock<IInformationCardRepo> mockRepository, IMapper mapper)
155+
{
156+
return new InformationCardsController(mockRepository.Object, mapper);
157+
}
158+
}
159+
}

Server.Tests/Server.Tests.csproj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12+
<PackageReference Include="Moq" Version="4.18.2" />
13+
<PackageReference Include="xunit" Version="2.4.1" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
<PrivateAssets>all</PrivateAssets>
17+
</PackageReference>
18+
<PackageReference Include="coverlet.collector" Version="3.1.0">
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
<PrivateAssets>all</PrivateAssets>
21+
</PackageReference>
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\Server\Server.csproj" />
26+
</ItemGroup>
27+
28+
</Project>

TexodeTestProject.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.1.32228.430
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{3967F8C4-1986-4FC4-B0A0-30593EBE2122}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{3967F8C4-1986-4FC4-B0A0-30593EBE2122}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{0A0BD151-F499-43D4-825E-E03944C9E042}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{0A0BD151-F499-43D4-825E-E03944C9E042}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server.Tests", "Server.Tests\Server.Tests.csproj", "{0B506EBD-F25D-4DB8-930C-AB5038E25938}"
911
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
2123
{0A0BD151-F499-43D4-825E-E03944C9E042}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{0A0BD151-F499-43D4-825E-E03944C9E042}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{0A0BD151-F499-43D4-825E-E03944C9E042}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{0B506EBD-F25D-4DB8-930C-AB5038E25938}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{0B506EBD-F25D-4DB8-930C-AB5038E25938}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{0B506EBD-F25D-4DB8-930C-AB5038E25938}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{0B506EBD-F25D-4DB8-930C-AB5038E25938}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)