CS2-27: Добавлена основа для xUnit тестов контроллеров

This commit is contained in:
KharchenkoVV 2021-07-19 11:58:17 +05:00
parent e99b476f47
commit 9b7dfe1d7e
3 changed files with 207 additions and 1 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp1", "ConsoleApp1\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsbCloudDb", "AsbCloudDb\AsbCloudDb.csproj", "{40FBD29B-724B-4496-B5D9-1A5D14102456}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataTable", "DataTable\DataTable.csproj", "{28AD7CD5-17A0-448C-8C16-A34AE5DE40FB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataTable", "DataTable\DataTable.csproj", "{28AD7CD5-17A0-448C-8C16-A34AE5DE40FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsbCloudWebApi.Tests", "AsbCloudWebApi.Tests\AsbCloudWebApi.Tests.csproj", "{9CF6FBB1-9AF5-45AB-A521-24F11A79B540}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{28AD7CD5-17A0-448C-8C16-A34AE5DE40FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28AD7CD5-17A0-448C-8C16-A34AE5DE40FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28AD7CD5-17A0-448C-8C16-A34AE5DE40FB}.Release|Any CPU.Build.0 = Release|Any CPU
{9CF6FBB1-9AF5-45AB-A521-24F11A79B540}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9CF6FBB1-9AF5-45AB-A521-24F11A79B540}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9CF6FBB1-9AF5-45AB-A521-24F11A79B540}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9CF6FBB1-9AF5-45AB-A521-24F11A79B540}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AsbCloudWebApi\AsbCloudWebApi.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using Xunit;
using Moq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using AsbCloudWebApi.Controllers;
using AsbCloudApp.Services;
using AsbCloudApp.Data;
namespace AsbCloudWebApi.Tests.ControllersTests
{
public class AnalyticsControllerTests
{
private readonly Mock<IAnalyticsService> analyticsService;
private readonly Mock<IWellService> wellService;
private readonly AnalyticsController controller;
private readonly List<WellDepthToDayDto> depthToDayDtos;
// fills class fields with data. Each test inside can change this data themselves for their needs
public AnalyticsControllerTests()
{
analyticsService = new Mock<IAnalyticsService>();
wellService = new Mock<IWellService>();
depthToDayDtos = new List<WellDepthToDayDto>()
{
new WellDepthToDayDto { WellDepth = 1000.0, BitDepth = 1000.0, Date = DateTime.Now },
new WellDepthToDayDto { WellDepth = 2000.0, BitDepth = 2000.0, Date = DateTime.Now },
new WellDepthToDayDto { WellDepth = 3000.0, BitDepth = 3000.0, Date = DateTime.Now }
};
analyticsService.Setup(s => s.GetWellDepthToDay(It.IsAny<int>()))
.Returns(depthToDayDtos);
wellService.Setup(s => s.CheckWellOwnership(It.IsAny<int>(), It.IsAny<int>()))
.Returns(true);
controller = new AnalyticsController(analyticsService.Object,
wellService.Object);
var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
{
new Claim("idCustomer", "1"),
}, "mock"));
controller.ControllerContext = new ControllerContext()
{
HttpContext = new DefaultHttpContext() { User = user }
};
}
[Fact]
public void It_should_return_depth_to_day_analytics()
{
var result = controller.GetWellDepthToDay(1);
var okResult = result as OkObjectResult;
Assert.NotNull(okResult);
}
[Fact]
public void It_should_return_correct_count_depth_to_day_analytics()
{
var result = controller.GetWellDepthToDay(1);
var okResult = result as OkObjectResult;
var resultCount = ((List<WellDepthToDayDto>)okResult.Value).Count;
Assert.Equal(3, resultCount);
}
[Fact]
public void It_should_return_403_if_no_idCustomer()
{
var emptyUserController = new AnalyticsController(analyticsService.Object,
wellService.Object);
var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { }, "mock"));
emptyUserController.ControllerContext = new ControllerContext()
{
HttpContext = new DefaultHttpContext() { User = user }
};
var result = emptyUserController.GetWellDepthToDay(1);
var forbidResult = result as ForbidResult;
Assert.NotNull(forbidResult);
}
[Fact]
public void It_should_return_403_if_user_doesnt_own_well()
{
var wellServiceReturnsFalse = new Mock<IWellService>();
wellServiceReturnsFalse.Setup(s => s.CheckWellOwnership(It.IsAny<int>(), It.IsAny<int>()))
.Returns(false);
var newControllerInstance = new AnalyticsController(analyticsService.Object,
wellServiceReturnsFalse.Object);
var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
{
new Claim("idCustomer", "1"),
}, "mock"));
newControllerInstance.ControllerContext = new ControllerContext()
{
HttpContext = new DefaultHttpContext() { User = user }
};
var result = newControllerInstance.GetWellDepthToDay(1);
var forbidResult = result as ForbidResult;
Assert.NotNull(forbidResult);
}
[Fact]
public void It_should_return_204_if_dtos_is_empty()
{
var emptyAnalyticsService = new Mock<IAnalyticsService>();
emptyAnalyticsService.Setup(s => s.GetWellDepthToDay(It.IsAny<int>()))
.Returns(new List<WellDepthToDayDto>());
var newControllerInstance = new AnalyticsController(emptyAnalyticsService.Object,
wellService.Object);
var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
{
new Claim("idCustomer", "1"),
}, "mock"));
newControllerInstance.ControllerContext = new ControllerContext()
{
HttpContext = new DefaultHttpContext() { User = user }
};
var result = newControllerInstance.GetWellDepthToDay(1);
var notFoundResult = result as NoContentResult;
Assert.NotNull(notFoundResult);
}
[Fact]
public void It_should_return_204_if_dtos_is_null()
{
var emptyAnalyticsService = new Mock<IAnalyticsService>();
emptyAnalyticsService.Setup(s => s.GetWellDepthToDay(It.IsAny<int>()))
.Returns(value: null);
var newControllerInstance = new AnalyticsController(emptyAnalyticsService.Object,
wellService.Object);
var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
{
new Claim("idCustomer", "1"),
}, "mock"));
newControllerInstance.ControllerContext = new ControllerContext()
{
HttpContext = new DefaultHttpContext() { User = user }
};
var result = newControllerInstance.GetWellDepthToDay(1);
var notFoundResult = result as NoContentResult;
Assert.NotNull(notFoundResult);
}
}
}