Esempio n. 1
0
        public async Task Handler_should_publish_notification()
        {
            var fakeSensor = new PortableSensor
            {
                Id       = 1,
                IsActive = false
            };
            var fakeSensorDbSet = new List <Sensor> {
                fakeSensor
            };

            _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(fakeSensorDbSet);
            _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object);

            var cancellationToken = new CancellationToken();
            var command           = new ChangeSensorActivationStateCommand(fakeSensor.Id, !fakeSensor.IsActive);
            var handler           =
                new ChangeSensorActivationStateCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object);

            //Act
            await handler.Handle(command, cancellationToken);

            //Assert
            _mediatorMock.Verify(x =>
                                 x.Publish(It.Is <SensorActivationStateChangedNotification>(it => it.SensorId == fakeSensor.Id),
                                           It.Is <CancellationToken>(it => it == cancellationToken)),
                                 Times.Once());
        }
Esempio n. 2
0
        public async Task Handler_should_return_true()
        {
            var fakeSensor = new PortableSensor
            {
                Id       = 1,
                IsActive = false
            };
            var fakeSensorDbSet = new List <Sensor> {
                fakeSensor
            };

            _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(fakeSensorDbSet);
            _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object);

            var cancellationToken = new CancellationToken();
            var command           = new ChangeSensorActivationStateCommand(fakeSensor.Id, !fakeSensor.IsActive);
            var handler           =
                new ChangeSensorActivationStateCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object);

            //Act
            var result = await handler.Handle(command, cancellationToken);

            //Assert
            Assert.True(result);
        }
Esempio n. 3
0
        public async Task Handler_should_throw_not_found_exception_if_sensor_doesnt_exist()
        {
            _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(new List <Sensor>());
            _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object);

            var cancellationToken = new CancellationToken();
            var command           = new ChangeSensorActivationStateCommand(1, false);
            var handler           =
                new ChangeSensorActivationStateCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object);

            //Act
            Task Act() => handler.Handle(command, cancellationToken);

            //Assert
            await Assert.ThrowsAsync <SensorNotFoundException>(Act);
        }
Esempio n. 4
0
        public async Task Handler_activation_state_should_be_changed(int sensorIndex, bool newActivationState)
        {
            //Check for both types of sensors
            var sensors = new List <Sensor>
            {
                new PortableSensor
                {
                    Id       = 1,
                    IsActive = !newActivationState
                },
                new StaticSensor
                {
                    Id       = 1,
                    IsActive = !newActivationState
                }
            };

            var fakeSensor      = sensors[sensorIndex];
            var fakeSensorDbSet = new List <Sensor> {
                fakeSensor
            };

            _dataContextMock.Setup(x => x.Sensors).ReturnsDbSet(fakeSensorDbSet);
            _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object);

            var cancellationToken = new CancellationToken();
            var command           = new ChangeSensorActivationStateCommand(fakeSensor.Id, newActivationState);
            var handler           =
                new ChangeSensorActivationStateCommandHandler(_dataContextFactoryMock.Object, _mediatorMock.Object);

            //Act
            await handler.Handle(command, cancellationToken);

            //Assert
            Assert.Equal(newActivationState, fakeSensor.IsActive);
        }