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.Messages.Services;
using Grand.Data;
using Grand.Domain.Messages;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Events;
using Grand.SharedKernel;
using Grand.Mediator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Business.Messages.Tests.Services;

[TestClass]
public class EmailAccountServiceTests
{
    private Mock<ICacheBase> _cacheMock;
    private Mock<IMediator> _mediatorMock;
    private Mock<IRepository<EmailAccount>> _repository;
    private EmailAccountService _service;

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

    [TestMethod]
    public async Task InsertEmailAccount_InvokeExpectedMethods()
    {
        await _service.InsertEmailAccount(new EmailAccount());
        _repository.Verify(c => c.InsertAsync(It.IsAny<EmailAccount>()), Times.Once);
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityInserted<EmailAccount>>(), default), Times.Once);
        _cacheMock.Verify(c => c.RemoveByPrefix(It.IsAny<string>(), It.IsAny<bool>()), Times.Once);
    }

    [TestMethod]
    public async Task UpdateEmailAccount_InvokeExpectedMethods()
    {
        await _service.UpdateEmailAccount(new EmailAccount());
        _repository.Verify(c => c.UpdateAsync(It.IsAny<EmailAccount>()), Times.Once);
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityUpdated<EmailAccount>>(), default), Times.Once);
        _cacheMock.Verify(c => c.RemoveByPrefix(It.IsAny<string>(), It.IsAny<bool>()), Times.Once);
    }

    [TestMethod]
    public async Task DeleteEmailAccount_InvokeExpectedMethods()
    {
        _repository.Setup(r => r.Table).Returns(new List<EmailAccount> { new(), new() }.AsQueryable());
        await _service.DeleteEmailAccount(new EmailAccount());
        _repository.Verify(c => c.DeleteAsync(It.IsAny<EmailAccount>()), Times.Once);
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityDeleted<EmailAccount>>(), default), Times.Once);
        _cacheMock.Verify(c => c.RemoveByPrefix(It.IsAny<string>(), It.IsAny<bool>()), Times.Once);
    }

    [TestMethod]
    public async Task DeleteEmailAccount_ExistOnlyOneAccount_ThrowException()
    {
        //we can't delete account if exist only one
        _repository.Setup(r => r.Table).Returns(new List<EmailAccount> { new() }.AsQueryable());
        await Assert.ThrowsExactlyAsync<GrandException>(async () => await _service.DeleteEmailAccount(new EmailAccount()));
    }
}