Пример #1
0
        public async Task AddCarrinhoAsync_invalid_item()
        {
            //arrange
            string clienteId = "123";
            var    repository
                = new RedisCarrinhoRepository(loggerMock.Object, redisMock.Object);

            //act
            //assert
            await Assert.ThrowsAsync <ArgumentNullException>(
                () => repository.AddCarrinhoAsync(clienteId, null));
        }
Пример #2
0
        public async Task AddCarrinhoAsync_negative_qty()
        {
            //arrange
            string clienteId = "123";
            var    repository
                = new RedisCarrinhoRepository(loggerMock.Object, redisMock.Object);

            //act
            //assert
            await Assert.ThrowsAsync <ArgumentOutOfRangeException>(
                () => repository.AddCarrinhoAsync(clienteId, new ItemCarrinho()
            {
                ProdutoId = "001", Quantidade = -1
            }));
        }
Пример #3
0
        public async Task AddCarrinhoAsync_success()
        {
            //arrange
            var json1 = JsonConvert.SerializeObject(new CarrinhoCliente("123")
            {
                Itens = new List <ItemCarrinho> {
                    new ItemCarrinho("001", "001", "produto 001", 12.34m, 1)
                }
            });
            var json2 = JsonConvert.SerializeObject(new CarrinhoCliente("123")
            {
                Itens = new List <ItemCarrinho> {
                    new ItemCarrinho("001", "001", "produto 001", 12.34m, 1), new ItemCarrinho("002", "002", "produto 002", 12.34m, 2)
                }
            });

            string clienteId    = "123";
            var    databaseMock = new Mock <IDatabase>();

            databaseMock
            .Setup(d => d.StringSetAsync(
                       It.IsAny <RedisKey>(),
                       It.IsAny <RedisValue>(),
                       null,
                       When.Always,
                       CommandFlags.None
                       ))
            .ReturnsAsync(true);
            databaseMock
            .SetupSequence(d => d.StringGetAsync(It.IsAny <RedisKey>(), It.IsAny <CommandFlags>()))
            .ReturnsAsync("")
            .ReturnsAsync(json1)
            .ReturnsAsync(json2);

            redisMock
            .Setup(r => r.GetDatabase(It.IsAny <int>(), It.IsAny <object>()))
            .Returns(databaseMock.Object)
            .Verifiable();

            var repository
                = new RedisCarrinhoRepository(loggerMock.Object, redisMock.Object);

            ItemCarrinho item = new ItemCarrinho("002", "002", "produto 002", 12.34m, 2);

            //act
            var carrinhoCliente = await repository.AddCarrinhoAsync(clienteId, item);

            //assert
            Assert.Equal(clienteId, carrinhoCliente.ClienteId);
            Assert.Collection(carrinhoCliente.Itens,
                              i =>
            {
                Assert.Equal("001", i.ProdutoId);
                Assert.Equal(1, i.Quantidade);
            },
                              i =>
            {
                Assert.Equal("002", i.ProdutoId);
                Assert.Equal(2, i.Quantidade);
            });
            databaseMock.Verify();
            redisMock.Verify();
        }