public async Task Soft_delete_entities_sets_DeletedOn_and_IsDeleted()
    {
        var entities = new List <TestEntity>()
        {
            new TestEntity()
        };

        var repoMock = new Mock <IWritableRepository <TestEntity> >();

        repoMock.Setup(r => r.Remove(It.IsAny <TestEntity>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);
        repoMock.Setup(r => r.RemoveRange(It.IsAny <List <TestEntity> >(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);
        repoMock.Setup(r => r.Update(It.IsAny <TestEntity>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);
        repoMock.Setup(r => r.UpdateRange(It.IsAny <List <TestEntity> >(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);

        var date            = new DateTime(2001, 2, 12);
        var dateTimeService = new DiscreteDateTimeService(date);

        var currentUserAccessorMock = new Mock <ICurrentUserAccessor>();

        currentUserAccessorMock.Setup(c => c.User)
        .Returns(new DomainUser("userName", Array.Empty <string>()));

        var decoratedRepo = repoMock.Object.AddSoftDeletability(dateTimeService, currentUserAccessorMock.Object);

        await decoratedRepo.RemoveRange(entities, default);

        Assert.Equal("userName", entities[0].DeletedBy);
        Assert.Equal(date, entities[0].DeletedOn);
        Assert.True(entities[0].IsDeleted);

        repoMock.Verify(r => r.UpdateRange(It.IsAny <List <TestEntity> >(), It.IsAny <CancellationToken>()), Times.Once);
        repoMock.VerifyNoOtherCalls();
    }
    public async Task Add_entity_sets_auditable_created_and_updated_properties()
    {
        var entity = new TestEntity();

        var repoMock = new Mock <IWritableRepository <TestEntity> >();

        repoMock.Setup(r => r.Add(It.IsAny <TestEntity>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(entity));

        var date            = new DateTime(2001, 2, 12);
        var dateTimeService = new DiscreteDateTimeService(date);

        var currentUserAccessorMock = new Mock <ICurrentUserAccessor>();

        currentUserAccessorMock.Setup(c => c.User)
        .Returns(new DomainUser("userName", Array.Empty <string>()));

        var decorator = repoMock.Object.AddAuditability(dateTimeService, currentUserAccessorMock.Object);

        await decorator.Add(entity, default);

        Assert.Equal("userName", entity.CreatedBy);
        Assert.Equal(date, entity.CreatedOn);
        Assert.Equal("userName", entity.ModifiedBy);
        Assert.Equal(date, entity.ModifiedOn);

        repoMock.Verify(r => r.Add(It.IsAny <TestEntity>(), It.IsAny <CancellationToken>()), Times.Once);
        repoMock.VerifyNoOtherCalls();
    }
Beispiel #3
0
    static Deps()
    {
        var userAccessorMock = new Mock <ICurrentUserAccessor>();

        userAccessorMock.Setup(a => a.User).Returns(new DomainUser("SingleUser", Array.Empty <string>()));
        CurrentUserAccessor = userAccessorMock.Object;

        var early = new DateTime(2001, 1, 1, 11, 11, 11, DateTimeKind.Utc);
        var late  = new DateTime(2002, 2, 2, 22, 22, 22, DateTimeKind.Utc);

        DateTimeServiceEarly = new DiscreteDateTimeService(early, new DateTimeOffset(early));
        DateTimeServiceLate  = new DiscreteDateTimeService(late, new DateTimeOffset(late));
    }
    public void Discrete_returns_the_specified_time()
    {
        var expected       = new DateTime(2000, 4, 2, 0, 0, 0);
        var expectedOffset = new DateTimeOffset(expected);

        var service = new DiscreteDateTimeService(expected, expectedOffset);

        var actual = service.Moment;

        Assert.Equal(expected, actual);
        Assert.Equal(DateTimeKind.Unspecified, actual.Kind);

        var actualOffset = service.MomentWithOffset;

        Assert.Equal(expectedOffset, actualOffset);
    }
    public async Task Update_entity_sets_auditable_updated_properties()
    {
        var entity = new TestEntity();

        var repoMock = new Mock <IWritableRepository <TestEntity> >();

        repoMock.Setup(r => r.Update(It.IsAny <TestEntity>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);

        var date            = new DateTime(2001, 2, 12);
        var dateTimeService = new DiscreteDateTimeService(date);

        var currentUserAccessorMock = new Mock <ICurrentUserAccessor>();

        currentUserAccessorMock.Setup(c => c.User)
        .Returns(new DomainUser("userName", Array.Empty <string>()));

        var decoratedRepo = repoMock.Object.AddAuditability(dateTimeService, currentUserAccessorMock.Object);

        await decoratedRepo.Update(entity, default);

        Assert.Equal(default, entity.CreatedBy);