public async Task Handler_should_update_database_with_sensor_with_provided_api_key() { //Arrange var apiKey = CryptoHelper.GenerateApiKey(); _dataContextMock.Setup(x => x.PortableSensors).ReturnsDbSet(new List <PortableSensor>()); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); var cancellationToken = new CancellationToken(); var command = new CreatePortableSensorCommand(apiKey); var handler = new CreatePortableSensorCommandHandler(_dataContextFactoryMock.Object, _mapperMock.Object); //Act await handler.Handle(command, cancellationToken); //Assert //Multiple assert because test is the same for both verified methods and they are very closely related _dataContextMock.Verify(x => x.PortableSensors.AddAsync(It.Is <PortableSensor>(it => it.ApiKey == apiKey), It.Is <CancellationToken>(it => it == cancellationToken)), Times.Once); _dataContextMock.Verify(x => x.SaveChangesAsync( It.Is <CancellationToken>(it => it == cancellationToken)), Times.Once); }
public async Task Handler_should_return_created_sensor_dto_with_provided_api_key() { //Arrange var apiKey = CryptoHelper.GenerateApiKey(); _dataContextMock.Setup(x => x.PortableSensors).ReturnsDbSet(new List <PortableSensor>()); _dataContextFactoryMock.Setup(x => x.Create()).Returns(_dataContextMock.Object); _mapperMock.Setup(x => x.Map <PortableSensor, PortableSensorDTO>(It.IsAny <PortableSensor>())).Returns( (PortableSensor v) => new PortableSensorDTO { ApiKey = v.ApiKey }); var cancellationToken = new CancellationToken(); var command = new CreatePortableSensorCommand(apiKey); var handler = new CreatePortableSensorCommandHandler(_dataContextFactoryMock.Object, _mapperMock.Object); //Act var result = await handler.Handle(command, cancellationToken); //Assert Assert.Equal(apiKey, result.ApiKey); }