// ========================================================================== // HandlerTestBase.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== using System; using System.Threading.Tasks; using Moq; using Squidex.Infrastructure.CQRS; using Squidex.Infrastructure.CQRS.Commands; namespace Squidex.Write.Utils { public abstract class HandlerTestBase where T : DomainObject { private readonly Mock domainObjectFactory = new Mock(); private readonly Mock domainObjectRepository = new Mock(); private readonly Guid id = Guid.NewGuid(); protected Guid Id { get { return id; } } protected Mock DomainObjectFactory { get { return domainObjectFactory; } } protected Mock DomainObjectRepository { get { return domainObjectRepository; } } public async Task TestCreate(T domainObject, Func action, bool succeeded = true) { domainObjectFactory.Setup(x => x.CreateNew(typeof(T), id)).Returns(domainObject).Verifiable(); domainObjectRepository.Setup(x => x.SaveAsync(domainObject, It.IsAny())).Returns(Task.FromResult(true)).Verifiable(); await action(domainObject); if (succeeded) { domainObjectFactory.VerifyAll(); domainObjectRepository.VerifyAll(); } } public async Task TestUpdate(T domainObject, Func action, bool succeeded = true) { domainObjectRepository.Setup(x => x.GetByIdAsync(domainObject.Id, int.MaxValue)).Returns(Task.FromResult(domainObject)).Verifiable(); domainObjectRepository.Setup(x => x.SaveAsync(domainObject, It.IsAny())).Returns(Task.FromResult(true)).Verifiable(); await action(domainObject); if (succeeded) { domainObjectRepository.VerifyAll(); } } } }