Skip to content

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.

2.6 KB
Raw Blame
using Grand.Business.Core.Interfaces.Customers;
using Grand.Business.Customers.Services;
using Grand.Data;
using Grand.Domain.Affiliates;
using Grand.Domain.Orders;
using Grand.Infrastructure.Events;
using Grand.Mediator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Business.Customers.Tests.Services;

[TestClass]
public class AffiliateServiceTests
{
    private Mock<IRepository<Affiliate>> _affiliateRepository;
    private IAffiliateService _affiliateService;
    private Mock<IMediator> _mediatorMock;
    private Mock<IRepository<Order>> _orderRepository;

    [TestInitialize]
    public void TestInitialize()
    {
        _affiliateRepository = new Mock<IRepository<Affiliate>>();
        _orderRepository = new Mock<IRepository<Order>>();
        _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()
    {
        await Assert.ThrowsExactlyAsync<ArgumentNullException>(
            async () => await _affiliateService.UpdateAffiliate(null), "affiliate");
    }

    [TestMethod]
    public async Task InsertAffiliate_NullParameter_ThrwoException()
    {
        await Assert.ThrowsExactlyAsync<ArgumentNullException>(
            async () => await _affiliateService.InsertAffiliate(null), "affiliate");
    }

    [TestMethod]
    public async Task DeleteAffiliate_NullParameter_ThrwoException()
    {
        await Assert.ThrowsExactlyAsync<ArgumentNullException>(
            async () => await _affiliateService.DeleteAffiliate(null), "affiliate");
    }


    [TestMethod]
    public async Task UpdateAffiliate_ValidParamters()
    {
        var affiliate = new Affiliate();
        await _affiliateService.UpdateAffiliate(affiliate);
        _affiliateRepository.Verify(c => c.UpdateAsync(It.IsAny<Affiliate>()), Times.Once);
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityUpdated<Affiliate>>(), default), Times.Once);
    }

    [TestMethod]
    public async Task InsertAffiliate_ValidParamters()
    {
        var affiliate = new Affiliate();
        await _affiliateService.InsertAffiliate(affiliate);
        _affiliateRepository.Verify(c => c.InsertAsync(It.IsAny<Affiliate>()), Times.Once);
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityInserted<Affiliate>>(), default), Times.Once);
    }
}