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.8 KB
Raw Blame
using Grand.Business.Checkout.Services.Shipping;
using Grand.Business.Core.Interfaces.Checkout.Shipping;
using Grand.Data;
using Grand.Domain.Shipping;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Events;
using Grand.Mediator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Business.Checkout.Tests.Services.Shipping;

[TestClass]
public class PickupPointServiceTests
{
    private Mock<ICacheBase> _cacheMock;
    private Mock<IMediator> _mediatorMock;
    private Mock<IRepository<PickupPoint>> _repositoryMock;
    private IPickupPointService _service;

    [TestInitialize]
    public void Init()
    {
        _repositoryMock = new Mock<IRepository<PickupPoint>>();
        _mediatorMock = new Mock<IMediator>();
        _cacheMock = new Mock<ICacheBase>();
        _service = new PickupPointService(_repositoryMock.Object, _mediatorMock.Object, _cacheMock.Object);
    }

    [TestMethod]
    public async Task InsertPickupPoint_InvokeExpectedMethods()
    {
        await _service.InsertPickupPoint(new PickupPoint());
        _repositoryMock.Verify(c => c.InsertAsync(It.IsAny<PickupPoint>()), Times.Once);
        _cacheMock.Verify(c => c.RemoveByPrefix(It.IsAny<string>(), It.IsAny<bool>()), Times.AtLeast(1));
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityInserted<PickupPoint>>(), default), Times.Once);
    }

    [TestMethod]
    public void InsertPickupPoint_NullArgument_ThrowException()
    {
        Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await _service.InsertPickupPoint(null));
    }

    [TestMethod]
    public async Task UpdatePickupPoint_InvokeExpectedMethods()
    {
        await _service.UpdatePickupPoint(new PickupPoint());
        _repositoryMock.Verify(c => c.UpdateAsync(It.IsAny<PickupPoint>()), Times.Once);
        _cacheMock.Verify(c => c.RemoveByPrefix(It.IsAny<string>(), It.IsAny<bool>()), Times.AtLeast(1));
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityUpdated<PickupPoint>>(), default), Times.Once);
    }

    [TestMethod]
    public void UpdatePickupPoint_NullArgument_ThrowException()
    {
        Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await _service.UpdatePickupPoint(null));
    }

    [TestMethod]
    public async Task DeletePickupPoint_InvokeExpectedMethods()
    {
        await _service.DeletePickupPoint(new PickupPoint());
        _repositoryMock.Verify(c => c.DeleteAsync(It.IsAny<PickupPoint>()), Times.Once);
        _cacheMock.Verify(c => c.RemoveByPrefix(It.IsAny<string>(), It.IsAny<bool>()), Times.AtLeast(1));
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityDeleted<PickupPoint>>(), default), Times.Once);
    }

    [TestMethod]
    public void DeletePickupPoint_NullArgument_ThrowException()
    {
        Assert.ThrowsExactlyAsync<ArgumentNullException>(async () => await _service.DeletePickupPoint(null));
    }
}