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

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

[TestClass]
public class OrderStatusServiceTests
{
    private Mock<ICacheBase> _cacheMock;
    private Mock<IMediator> _mediatorMock;
    private Mock<IRepository<OrderStatus>> _repositoryMock;
    private OrderStatusService _service;

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

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

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

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

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

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

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