diff --git a/UnitTesting.sln b/UnitTesting.sln index c8bbb64..0d78970 100644 --- a/UnitTesting.sln +++ b/UnitTesting.sln @@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTesting", "UnitTesting\ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestingLib", "TestingLib\TestingLib.csproj", "{E2FF35D4-3B82-45E0-BAB8-DE39777C4D53}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RealizationApp", "RealizationApp\RealizationApp.csproj", "{6301EAB5-B771-4477-9242-84D8C2944114}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RealizationApp", "RealizationApp\RealizationApp.csproj", "{6301EAB5-B771-4477-9242-84D8C2944114}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/UnitTesting/BasicCalcTest.cs b/UnitTesting/BasicCalcTest.cs index e044d1d..d04963e 100644 --- a/UnitTesting/BasicCalcTest.cs +++ b/UnitTesting/BasicCalcTest.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.VisualStudio.TestPlatform.TestHost; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -34,5 +35,6 @@ public void Add_Theory(int a, int b, int expectedResult) int result = _calculator.Add(a, b); Assert.Equal(expectedResult, result); } + } } diff --git a/UnitTesting/GSEvgenievich/MoqTests.cs b/UnitTesting/GSEvgenievich/MoqTests.cs new file mode 100644 index 0000000..8d5799d --- /dev/null +++ b/UnitTesting/GSEvgenievich/MoqTests.cs @@ -0,0 +1,95 @@ +using Moq; +using TestingLib.Shop; +using TestingLib.Weather; + +namespace UnitTesting.GSEvgenievich +{ + public class MoqTests + { + private readonly Mock mockCustomerRepository; + private readonly Mock mockOrderRepository; + private readonly Mock mockNotificationService; + private readonly Mock mockWeatherForecastSource; + public MoqTests() + { + mockCustomerRepository = new Mock(); + mockOrderRepository = new Mock(); + mockNotificationService = new Mock(); + mockWeatherForecastSource = new Mock(); + } + + [Fact] + public void GetCustomerInfo_ShouldReturnCorrectInfo() + { + // Arrange + var customer = new Customer { Id = 1, Name = "Petya", Email = "petya@yandex.ru" }; + + mockCustomerRepository.Setup(repo => repo.GetCustomerById(1)).Returns(customer); + mockOrderRepository.Setup(repo => repo.GetOrders()).Returns(new List { new Order() { Customer = customer }, new Order() { Customer = customer } }); + + var service = new ShopService(mockCustomerRepository.Object, mockOrderRepository.Object, mockNotificationService.Object); + + // Act + var result = service.GetCustomerInfo(1); + + // Assert + Assert.Equal("Customer " + customer.Name + " has 2 orders", result); + mockOrderRepository.Verify(repo => repo.GetOrders(), Times.Once); + mockCustomerRepository.Verify(repo => repo.GetCustomerById(It.IsAny()), Times.Once); + } + + [Fact] + public void CreateOrder_ShouldAddOrder() + { + // Arrange + var customer = new Customer { Id = 1, Name = "Petya", Email = "petya@yandex.ru" }; + var order = new Order { Id = 2, Date = DateTime.Now, Customer = customer, Amount = 3 }; + mockOrderRepository.Setup(repo => repo.GetOrderById(1)).Returns(order); + + var service = new ShopService(mockCustomerRepository.Object, mockOrderRepository.Object, mockNotificationService.Object); + + // Act + service.CreateOrder(order); + + // Act и Assert + mockOrderRepository.Verify(repo => repo.GetOrderById(It.IsAny()), Times.Once); + mockOrderRepository.Verify(repo => repo.AddOrder(It.IsAny()), Times.Once); + } + + [Fact] + public void CreateOrder_ShouldSendNotification() + { + // Arrange + var customer = new Customer { Id = 1, Name = "Petya", Email = "petya@yandex.ru" }; + var order = new Order { Id = 2, Date = DateTime.Now, Customer = customer, Amount = 3 }; + mockOrderRepository.Setup(repo => repo.GetOrderById(1)).Returns(order); + + var service = new ShopService(mockCustomerRepository.Object, mockOrderRepository.Object, mockNotificationService.Object); + + // Act + service.CreateOrder(order); + + // Act и Assert + mockNotificationService.Verify(repo => repo.SendNotification(It.IsAny(), It.IsAny()), Times.Once); + } + + [Fact] + public void GetWeatherForecast_ShouldReturnCorrectInfo() + { + var weatherForecast = new WeatherForecast { Summary = "Yasno", TemperatureC = 23 }; + var currentTime = DateTime.Now; + mockWeatherForecastSource.Setup(repo => repo.GetForecast(currentTime)).Returns(weatherForecast); + + var service = new WeatherForecastService(mockWeatherForecastSource.Object); + + //Act + var result = service.GetWeatherForecast(currentTime); + + //Assert + Assert.NotNull(result); + mockWeatherForecastSource.Verify(repo => repo.GetForecast(It.IsAny()), Times.Once); + } + } +} + + diff --git a/UnitTesting/GSEvgenievich/README.md b/UnitTesting/GSEvgenievich/README.md new file mode 100644 index 0000000..1b46362 --- /dev/null +++ b/UnitTesting/GSEvgenievich/README.md @@ -0,0 +1,2 @@ + . . + . . \ No newline at end of file diff --git a/UnitTesting/GSEvgenievich/README.txt b/UnitTesting/GSEvgenievich/README.txt new file mode 100644 index 0000000..d610b96 --- /dev/null +++ b/UnitTesting/GSEvgenievich/README.txt @@ -0,0 +1 @@ +ИСПП-35: Волков Н. и Пожидаев Г. \ No newline at end of file diff --git a/UnitTesting/GSEvgenievich/Tests.cs b/UnitTesting/GSEvgenievich/Tests.cs new file mode 100644 index 0000000..d4a8e99 --- /dev/null +++ b/UnitTesting/GSEvgenievich/Tests.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TestingLib.Math; + +namespace UnitTesting.GSEvgenievich +{ + public class Tests + { + private readonly BasicCalc _calculator; + + public Tests() + { + _calculator = new BasicCalc(); + } + + [Fact] + public void LCM_ShouldReturnCorrectLargestMultipleNumber() + { + int result = _calculator.LCM(18, 24); + Assert.Equal(72, result); + } + + + [Theory] + [InlineData(10000, 100, 10000)] + [InlineData(1, 2, 2)] + [InlineData(5, 9, 45)] + public void LCM_Theory(int a, int b, int expectedResult) + { + int result = _calculator.LCM(a, b); + Assert.Equal(expectedResult, result); + } + + [Fact] + public void LCM_ShouldThrowArgumentOutOfRangeException() + { + Assert.Throws(() => _calculator.LCM(-10, 10)); + } + + [Fact] + public void IsPerfectNumber_ShouldReturnTrue() + { + bool result = _calculator.IsPerfectNumber(6); + Assert.Equal(true, result); + } + + + [Theory] + [InlineData(19, false)] + [InlineData(28, true)] + [InlineData(496, true)] + public void IsPerfectNumber_Theory(int a, bool expectedResult) + { + bool result = _calculator.IsPerfectNumber(a); + Assert.Equal(expectedResult, result); + } + + [Fact] + public void IsPerfectNumber_ShouldThrowArgumentOutOfRangeException() + { + Assert.Throws(() => _calculator.IsPerfectNumber(-10)); + } + } +} diff --git a/UnitTesting/GSEvgenievich/UnitTest.cs b/UnitTesting/GSEvgenievich/UnitTest.cs new file mode 100644 index 0000000..52e61b5 --- /dev/null +++ b/UnitTesting/GSEvgenievich/UnitTest.cs @@ -0,0 +1,74 @@ +using Microsoft.VisualStudio.TestPlatform.TestHost; +using TestingLib.Math; + +namespace UnitTesting.GSEvgenievich +{ + public class UnitTest + { + private readonly BasicCalc _calculator; + + public UnitTest() + { + _calculator = new BasicCalc(); + } + + // Задание 1 + [Fact] + public void LCM_PositiveNumbers_ReturnsCorrectLCM() + { + int a = 12; + int b = 15; + int expectedResult = 60; + + int result = _calculator.LCM(a, b); + + Assert.Equal(expectedResult, result); + } + + [Theory] + [InlineData(2, 3)] + [InlineData(4, 6)] + [InlineData(7, 11)] + public void LCM_MultipleInputs_ReturnsCorrectLCM(int a, int b) + { + int result = _calculator.LCM(a, b); + + Assert.True(result % a == 0 && result % b == 0); + } + + [Fact] + public void LCM_NegativeNumbers_ThrowsException() + { + Assert.Throws(() => _calculator.LCM(-13, 16)); + } + + // Задание 2 + [Fact] + public void IsPrime_ValidInput_ReturnsCorrectResult() + { + int n = 7; + + bool result = _calculator.IsPrime(n); + + Assert.True(result); + } + + [Theory] + [InlineData(1,true)] + [InlineData(3, true)] + [InlineData(5, true)] + [InlineData(11, true)] + public void IsPrime_MultipleInputs_ReturnsExpectedResults(int n,bool expectedResult) + { + bool result = _calculator.IsPrime(n); + + Assert.Equal(expectedResult, result); + } + + [Fact] + public void IsPrime_InvalidInput_ThrowsArgumentOutOfRangeException() + { + Assert.Throws(() => _calculator.IsPrime(-1)); + } + } +}