Esempio n. 1
0
        public void Setup()
        {
            repository = Substitute.For <IDbRepository>();
            var auditTrailWriter = Substitute.For <IAuditTrailWriter>();

            sut = new CreateRoleCommandHandler(repository, auditTrailWriter);
        }
Esempio n. 2
0
 public RolController(
     CreateRoleCommandHandler createCommand,
     GetAllRolesQuery getAllQuery,
     GetRoleQuery getOneQuery,
     UpdateRoleCommandHandler updateCommand,
     DeleteRoleCommandHandler deleteCommand)
 {
     this.createCommand = createCommand;
     this.getAllQuery   = getAllQuery;
     this.getOneQuery   = getOneQuery;
     this.updateCommand = updateCommand;
     this.deleteCommand = deleteCommand;
 }
        public async Task Handle_GivenSavingSucceeds_ExpectSuccessfulResult()
        {
            var roleQueries = new Mock <IRoleQueries>();

            roleQueries.Setup(x => x.CheckForPresenceOfRoleByName(It.IsAny <string>()))
            .ReturnsAsync(() => new StatusCheckModel(false));
            var roleRepository = new Mock <IRoleRepository>();
            var unitOfWork     = new Mock <IUnitOfWork>();

            unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true);
            roleRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object);

            var handler = new CreateRoleCommandHandler(roleRepository.Object, roleQueries.Object);
            var cmd     = new CreateRoleCommand("name", new List <Guid>());

            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsSuccess);
        }
        public async Task Handle_GivenRoleAlreadyExists_ExpectFailedResult()
        {
            var roleQueries = new Mock <IRoleQueries>();

            roleQueries.Setup(x => x.CheckForPresenceOfRoleByName(It.IsAny <string>()))
            .ReturnsAsync(() => new StatusCheckModel(true));
            var roleRepository = new Mock <IRoleRepository>();
            var unitOfWork     = new Mock <IUnitOfWork>();

            unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true);
            roleRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object);

            var handler = new CreateRoleCommandHandler(roleRepository.Object, roleQueries.Object);
            var cmd     = new CreateRoleCommand("name", new List <Guid>());

            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsFailure);
            Assert.Equal(ErrorCodes.RoleAlreadyExists, result.Error.Code);
            roleRepository.Verify(x => x.Add(It.IsAny <IRole>()), Times.Never);
        }
        public async Task Handle_GivenRoleDoesNotExists_ExpectSuccessfulResultWithIdSetAndResourcesSetAndRoleAdded()
        {
            var roleId      = Guid.Empty;
            var roleQueries = new Mock <IRoleQueries>();

            roleQueries.Setup(x => x.CheckForPresenceOfRoleByName(It.IsAny <string>()))
            .ReturnsAsync(() => new StatusCheckModel(false));
            var roleRepository = new Mock <IRoleRepository>();
            var unitOfWork     = new Mock <IUnitOfWork>();

            unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true);
            roleRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object);
            roleRepository.Setup(x => x.Add(It.IsAny <IRole>())).Callback((IRole role) => { roleId = role.Id; });

            var handler = new CreateRoleCommandHandler(roleRepository.Object, roleQueries.Object);
            var cmd     = new CreateRoleCommand("name", new List <Guid>());

            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsSuccess);
            Assert.Equal(roleId, result.Value.RoleId);
            roleRepository.Verify(x => x.Add(It.IsAny <IRole>()), Times.Once);
        }
 public void Setup()
 {
     repository = Substitute.For<IDbRepository>();
     var auditTrailWriter = Substitute.For<IAuditTrailWriter>();
     sut = new CreateRoleCommandHandler(repository, auditTrailWriter);
 }