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 DeliveryDateServiceTests
{
    private Mock<ICacheBase> _cacheMock;
    private Mock<IMediator> _mediatorMock;
    private Mock<IRepository<DeliveryDate>> _repositoryMock;
    private IDeliveryDateService _service;

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

    [TestMethod]
    public async Task InsertDeliveryDate_InvokeExpectedMethods()
    {
        await _service.InsertDeliveryDate(new DeliveryDate());
        _repositoryMock.Verify(c => c.InsertAsync(It.IsAny<DeliveryDate>()), Times.Once);
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityInserted<DeliveryDate>>(), default), Times.Once);
    }

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

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

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

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

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