public async Task GetBasketAsync_invalid_customerId() { //arrange string customerId = ""; var repository = new RedisBasketRepository(loggerMock.Object, redisMock.Object); //act - assert await Assert.ThrowsAsync <ArgumentException>( () => repository.GetBasketAsync(customerId)); }
public async Task GetBasketAsync_customerId_NotFound() { //arrange var json = @"{ ""CustomerId"": ""123"", ""Items"": [] }"; string customerId = "123"; var databaseMock = new Mock <IDatabase>(); databaseMock .Setup(d => d.StringSetAsync( It.IsAny <RedisKey>(), It.IsAny <RedisValue>(), null, When.Always, CommandFlags.None )) .ReturnsAsync(true) .Verifiable(); databaseMock.SetupSequence(d => d.StringGetAsync(It.IsAny <RedisKey>(), It.IsAny <CommandFlags>())) .ReturnsAsync("") .ReturnsAsync(json); redisMock .Setup(r => r.GetDatabase(It.IsAny <int>(), It.IsAny <object>())) .Returns(databaseMock.Object) .Verifiable(); var repository = new RedisBasketRepository(loggerMock.Object, redisMock.Object); //act var customerBasket = await repository.GetBasketAsync(customerId); //assert Assert.Equal(customerId, customerBasket.CustomerId); Assert.Empty(customerBasket.Items); databaseMock.Verify(); redisMock.Verify(); }
public async Task GetBasketAsync_success() { //arrange var json = @"{ ""CustomerId"": ""123"", ""Items"": [{ ""Id"": ""001"", ""ProductId"": ""001"", ""ProdutoNome"": ""Produto 001"", ""Quantidade"": 7, ""PrecoUnitario"": 12.34}] }"; string customerId = "123"; var databaseMock = new Mock <IDatabase>(); databaseMock .Setup(d => d.StringGetAsync(It.IsAny <RedisKey>(), It.IsAny <CommandFlags>())) .ReturnsAsync(json) .Verifiable(); redisMock .Setup(r => r.GetDatabase(It.IsAny <int>(), It.IsAny <object>())) .Returns(databaseMock.Object) .Verifiable(); var repository = new RedisBasketRepository(loggerMock.Object, redisMock.Object); //act var customerBasket = await repository.GetBasketAsync(customerId); //assert Assert.Equal(customerId, customerBasket.CustomerId); Assert.Collection(customerBasket.Items, item => { Assert.Equal("001", item.ProductId); Assert.Equal(7, item.Quantity); }); databaseMock.Verify(); redisMock.Verify(); }