Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Add tests
supportbe3263c9fbb8
diff --git a/src/Tests/Grand.Business.Customers.Tests/Services/AffiliateServiceTests.cs b/src/Tests/Grand.Business.Customers.Tests/Services/AffiliateServiceTests.csindex e0bbb25f0..f40df7798 100644--- a/src/Tests/Grand.Business.Customers.Tests/Services/AffiliateServiceTests.cs+++ b/src/Tests/Grand.Business.Customers.Tests/Services/AffiliateServiceTests.cs@@ -26,6 +26,13 @@ namespace Grand.Business.Customers.Tests.Services_mediatorMock = new Mock<IMediator>();_affiliateService = new AffiliateService(_affiliateRepository.Object, _orderRepository.Object, _mediatorMock.Object);}+ [TestMethod()]+ public async Task GetAffiliateByIdTest()+ {+ await _affiliateService.GetAffiliateById("");+ _affiliateRepository.Verify(c => c.GetByIdAsync(It.IsAny<string>()), Times.Once);++ }[TestMethod()]public async Task UpdateAffiliate_NullParameter_ThrwoException()diff --git a/src/Tests/Grand.Business.Customers.Tests/Services/CustomerAttributeParserTests.cs b/src/Tests/Grand.Business.Customers.Tests/Services/CustomerAttributeParserTests.csindex a01d3819e..a0f1d6a7b 100644--- a/src/Tests/Grand.Business.Customers.Tests/Services/CustomerAttributeParserTests.cs+++ b/src/Tests/Grand.Business.Customers.Tests/Services/CustomerAttributeParserTests.cs@@ -12,16 +12,20 @@ namespace Grand.Business.Customers.Tests.Servicespublic class CustomerAttributeParserTests{private Mock<ICustomerAttributeService> _customerAtrServiceMock;- private Mock<ITranslationService> _translationServiceMock;private CustomerAttributeParser _parser;private List<CustomAttribute> customAtr;- private List<CustomerAttribute> _cusomerAtr;+ private List<CustomerAttribute> _customerAtr;[TestInitialize()]public void Init(){_customerAtrServiceMock = new Mock<ICustomerAttributeService>();- _translationServiceMock = new Mock<ITranslationService>();+ //_translationServiceMock = new Mock<ITranslationService>();+ var _translationServiceMock = new Mock<ITranslationService>();+ {+ _translationServiceMock.Setup(x => x.GetResource(It.IsAny<string>())).Returns("Warning{0}");+ }+_parser = new CustomerAttributeParser(_customerAtrServiceMock.Object, _translationServiceMock.Object);customAtr = new List<CustomAttribute>(){@@ -31,21 +35,21 @@ namespace Grand.Business.Customers.Tests.Servicesnew CustomAttribute(){Key="key4",Value="value4" },};- _cusomerAtr = new List<CustomerAttribute>()+ _customerAtr = new List<CustomerAttribute>(){- new CustomerAttribute(){Id="key1"},- new CustomerAttribute(){Id="key2"},- new CustomerAttribute(){Id="key3"},- new CustomerAttribute(){Id="key4"},- new CustomerAttribute(){Id="key5"},+ new CustomerAttribute(){Id="key1", Name="name1", AttributeControlTypeId = Domain.Catalog.AttributeControlType.TextBox},+ new CustomerAttribute(){Id="key2", Name="name2"},+ new CustomerAttribute(){Id="key3", Name="name3"},+ new CustomerAttribute(){Id="key4", Name="name4"},+ new CustomerAttribute(){Id="key5", Name="name5"},};- _cusomerAtr.ForEach(c => c.CustomerAttributeValues.Add(new CustomerAttributeValue() { Id = "value" + c.Id.Last() }));+ _customerAtr.ForEach(c => c.CustomerAttributeValues.Add(new CustomerAttributeValue() { Id = "value" + c.Id.Last() }));}[TestMethod()]public async Task ParseCustomerAttributes_ReturnExpectedValues(){- _customerAtrServiceMock.Setup(c => c.GetCustomerAttributeById(It.IsAny<string>())).Returns((string w) => Task.FromResult(_cusomerAtr.FirstOrDefault(a => a.Id.Equals(w))));+ _customerAtrServiceMock.Setup(c => c.GetCustomerAttributeById(It.IsAny<string>())).Returns((string w) => Task.FromResult(_customerAtr.FirstOrDefault(a => a.Id.Equals(w))));var result = await _parser.ParseCustomerAttributes(customAtr);Assert.IsTrue(result.Count == 4);Assert.IsTrue(result.Any(c => c.Id.Equals("key1")));@@ -65,9 +69,9 @@ namespace Grand.Business.Customers.Tests.Services}[TestMethod()]- public async Task ParseCustomerAttributeValues_ReturnExpectedValues()+ public async Task ParseCustomer0AttributeValues_ReturnExpectedValues(){- _customerAtrServiceMock.Setup(c => c.GetCustomerAttributeById(It.IsAny<string>())).Returns((string w) => Task.FromResult(_cusomerAtr.FirstOrDefault(a => a.Id.Equals(w))));+ _customerAtrServiceMock.Setup(c => c.GetCustomerAttributeById(It.IsAny<string>())).Returns((string w) => Task.FromResult(_customerAtr.FirstOrDefault(a => a.Id.Equals(w))));var result = await _parser.ParseCustomerAttributeValues(customAtr);Assert.IsTrue(result.Count == 4);Assert.IsTrue(result.Any(c => c.Id.Equals("value1")));@@ -87,5 +91,34 @@ namespace Grand.Business.Customers.Tests.ServicesAssert.IsTrue(result.Count == 5);Assert.IsTrue(result.Any(c => c.Key.Equals("key7")));}++ [TestMethod()]+ public async Task GetAttributeWarnings_Return_NoWarning()+ {+ _customerAtrServiceMock.Setup(c => c.GetAllCustomerAttributes()).Returns(() => Task.FromResult((IList<CustomerAttribute>)_customerAtr));+ _customerAtrServiceMock.Setup(c => c.GetCustomerAttributeById(It.IsAny<string>())).Returns((string w) => Task.FromResult(_customerAtr.FirstOrDefault(a => a.Id.Equals(w))));+ var result = await _parser.GetAttributeWarnings(customAtr);+ Assert.IsTrue(result.Count == 0);+ }+ [TestMethod()]+ public async Task GetAttributeWarnings_Return_WithWarning()+ {+ _customerAtr.FirstOrDefault(x=>x.Id == "key5").IsRequired = true;+ _customerAtrServiceMock.Setup(c => c.GetAllCustomerAttributes()).Returns(() => Task.FromResult((IList<CustomerAttribute>)_customerAtr));+ _customerAtrServiceMock.Setup(c => c.GetCustomerAttributeById(It.IsAny<string>())).Returns((string w) => Task.FromResult(_customerAtr.FirstOrDefault(a => a.Id.Equals(w))));+ var result = await _parser.GetAttributeWarnings(customAtr);+ Assert.IsTrue(result.Count == 1);+ }+ [TestMethod()]+ public async Task FormatAttributes_Return_string()+ {+ _customerAtrServiceMock.Setup(c => c.GetAllCustomerAttributes()).Returns(() => Task.FromResult((IList<CustomerAttribute>)_customerAtr));+ _customerAtrServiceMock.Setup(c => c.GetCustomerAttributeById(It.IsAny<string>())).Returns((string w) => Task.FromResult(_customerAtr.FirstOrDefault(a => a.Id.Equals(w))));+ var result = await _parser.FormatAttributes(new Domain.Localization.Language(),+ new List<CustomAttribute>() {+ new CustomAttribute(){Key="key1",Value="value1" }+ });+ Assert.IsTrue(result == "name1: value1");+ }}}diff --git a/src/Tests/Grand.Business.Customers.Tests/Services/CustomerAttributeServiceTests.cs b/src/Tests/Grand.Business.Customers.Tests/Services/CustomerAttributeServiceTests.csindex 33df32592..dff65e452 100644--- a/src/Tests/Grand.Business.Customers.Tests/Services/CustomerAttributeServiceTests.cs+++ b/src/Tests/Grand.Business.Customers.Tests/Services/CustomerAttributeServiceTests.cs@@ -52,5 +52,41 @@ namespace Grand.Business.Customers.Tests.Services_mediatorMock.Verify(c => c.Publish(It.IsAny<EntityDeleted<CustomerAttribute>>(), default(CancellationToken)), Times.Once);_cacheMock.Verify(c => c.RemoveByPrefix(It.IsAny<string>(), true));}++ [TestMethod()]+ public async Task InsertCustomerAttributeValue_ValidArguemnts_InvokeRepositoryAndCache()+ {+ _repositoryMock.Setup(c => c.GetByIdAsync(It.IsAny<string>())).Returns(() => Task.FromResult(new CustomerAttribute()));+ await _atrService.InsertCustomerAttributeValue(new CustomerAttributeValue());+ _repositoryMock.Verify(c => c.UpdateAsync(It.IsAny<CustomerAttribute>()), Times.Once);+ _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityInserted<CustomerAttributeValue>>(), default(CancellationToken)), Times.Once);+ _cacheMock.Verify(c => c.RemoveByPrefix(It.IsAny<string>(), true));+ }++ [TestMethod()]+ public async Task UpdateCustomerAttributeValue_ValidArguemnts_InvokeRepositoryAndCache()+ {+ _repositoryMock.Setup(c => c.GetByIdAsync(It.IsAny<string>())).Returns(() => Task.FromResult(new CustomerAttribute()));+ await _atrService.UpdateCustomerAttributeValue(new CustomerAttributeValue());+ _repositoryMock.Verify(c => c.UpdateAsync(It.IsAny<CustomerAttribute>()), Times.Once);+ _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityUpdated<CustomerAttributeValue>>(), default(CancellationToken)), Times.Once);+ _cacheMock.Verify(c => c.RemoveByPrefix(It.IsAny<string>(), true));+ }+ [TestMethod()]+ public async Task DeleteCustomerAttributeValue_ValidArguemnts_InvokeRepositoryAndCache()+ {+ _repositoryMock.Setup(c => c.GetByIdAsync(It.IsAny<string>())).Returns(() => Task.FromResult(new CustomerAttribute()));+ await _atrService.DeleteCustomerAttributeValue(new CustomerAttributeValue());+ _repositoryMock.Verify(c => c.UpdateAsync(It.IsAny<CustomerAttribute>()), Times.Once);+ _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityDeleted<CustomerAttributeValue>>(), default(CancellationToken)), Times.Once);+ _cacheMock.Verify(c => c.RemoveByPrefix(It.IsAny<string>(), true));+ }++ [TestMethod()]+ public async Task GetAllCustomerAttributes_InvokeRepositoryAndCache()+ {+ await _atrService.GetAllCustomerAttributes();+ _cacheMock.Verify(c => c.GetAsync(It.IsAny<string>(), It.IsAny<Func<Task<List<CustomerAttribute>>>>()), Times.Once);+ }}}diff --git a/src/Tests/Grand.Business.Customers.Tests/Services/CustomerNoteServiceTests.cs b/src/Tests/Grand.Business.Customers.Tests/Services/CustomerNoteServiceTests.csindex 9898dda0e..bb66f471c 100644--- a/src/Tests/Grand.Business.Customers.Tests/Services/CustomerNoteServiceTests.cs+++ b/src/Tests/Grand.Business.Customers.Tests/Services/CustomerNoteServiceTests.cs@@ -22,7 +22,20 @@ namespace Grand.Business.Customers.Tests.Services_mediatorMock = new Mock<IMediator>();_noteService = new CustomerNoteService(_repositoryMock.Object, _mediatorMock.Object);}+ [TestMethod()]+ public async Task GetCustomerNoteTest()+ {+ await _noteService.GetCustomerNote("1");+ _repositoryMock.Verify(c => c.GetByIdAsync(It.IsAny<string>()), Times.Once);+ }+ [TestMethod()]+ public async Task GetCustomerNoteTest_Null()+ {+ await _noteService.GetCustomerNote("");+ _repositoryMock.Verify(c => c.GetByIdAsync(It.IsAny<string>()), Times.Never);++ }[TestMethod()]public async Task InsertCustomerNote_ValidArguments_InvokeRepositoryAndPublishEvent(){diff --git a/src/Tests/Grand.Business.Customers.Tests/Services/UserApiServiceTests.cs b/src/Tests/Grand.Business.Customers.Tests/Services/UserApiServiceTests.csindex ba503fb19..9d7934894 100644--- a/src/Tests/Grand.Business.Customers.Tests/Services/UserApiServiceTests.cs+++ b/src/Tests/Grand.Business.Customers.Tests/Services/UserApiServiceTests.cs@@ -23,6 +23,13 @@ namespace Grand.Business.Customers.Tests.Services_userApiService = new UserApiService(_repositoryMock.Object, _mediatorMock.Object);}+ [TestMethod()]+ public async Task GetUserByIdTest()+ {+ await _userApiService.GetUserById("");+ _repositoryMock.Verify(c => c.GetByIdAsync(It.IsAny<string>()), Times.Once);++ }[TestMethod()]public async Task InsertUserApi_ValidArguments_InvokeRepositoryAndPublishEvent(){diff --git a/src/Tests/Grand.Business.Customers.Tests/Services/VendorServiceTests.cs b/src/Tests/Grand.Business.Customers.Tests/Services/VendorServiceTests.csindex dd8597058..811a13cde 100644--- a/src/Tests/Grand.Business.Customers.Tests/Services/VendorServiceTests.cs+++ b/src/Tests/Grand.Business.Customers.Tests/Services/VendorServiceTests.cs@@ -24,7 +24,13 @@ namespace Grand.Business.Customers.Tests.Services_mediatorMock = new Mock<IMediator>();_vendorService = new VendorService(_repoMock.Object, _vendorReviewRepositoryMock.Object, _mediatorMock.Object);}+ [TestMethod()]+ public async Task GetVendorByIdTest()+ {+ await _vendorService.GetVendorById("");+ _repoMock.Verify(c => c.GetByIdAsync(It.IsAny<string>()), Times.Once);+ }[TestMethod()]public async Task InsertVendor_ValidArguments_InvokeRepositoryAndPublishEvent(){diff --git a/src/Tests/Grand.Business.Customers.Tests/Startup/StartupApplicationTests.cs b/src/Tests/Grand.Business.Customers.Tests/Startup/StartupApplicationTests.csnew file mode 100644index 000000000..9ab3739bd--- /dev/null+++ b/src/Tests/Grand.Business.Customers.Tests/Startup/StartupApplicationTests.cs@@ -0,0 +1,31 @@+using Microsoft.Extensions.Configuration;+using Microsoft.Extensions.DependencyInjection;+using Microsoft.VisualStudio.TestTools.UnitTesting;++namespace Grand.Business.Customers.Startup.Tests+{+ [TestClass()]+ public class StartupApplicationTests+ {+ StartupApplication _application;+ ServiceCollection _serviceCollection;+ IConfiguration _configuration;++ [TestInitialize]+ public void Init()+ {+ _serviceCollection = new ServiceCollection();+ _configuration = new ConfigurationBuilder().Build();+ _application = new StartupApplication();++ }+ [TestMethod()]+ public void ConfigureServicesTest()+ {+ //Act+ _application.ConfigureServices(_serviceCollection, _configuration);+ //Assert+ Assert.IsTrue(_serviceCollection.Count > 0);+ }+ }+}\ No newline at end of file