示例#1
0
        public void TodosControlesDevemHerdarDeController()
        {
            /* Neste caso não foi possível testas utlizando mocks, pois a interface não pode herdar da classe "Controller" */
            var homeController = new HomeController();
            var productController = new ProductController(new Mock<IProductBusiness>().Object);
            var accountController = new AccountController(new Mock<IAccountBusiness>().Object);
            var carrinhoDeComprasController = new CarrinhoDeComprasController(new Mock<IOrderBusiness>().Object);

            Assert.IsInstanceOf(typeof(Controller), homeController);
            Assert.IsInstanceOf(typeof(Controller), productController);
            Assert.IsInstanceOf(typeof(Controller), accountController);
            Assert.IsInstanceOf(typeof(Controller), carrinhoDeComprasController);
        }
        public void AoAcessarAPaginaDeListagemEOcorrerUmaExcecaoNaCamadaDeNegocios_OSistemaDeveNotificarAoUsuario()
        {
            var business = new Mock<IProductBusiness>();
            business.Setup(x => x.GetActiveProducts()).Throws<Exception>();

            _controller = new HomeController(business.Object);

            var result = _controller.Index() as ViewResult;

            Assert.NotNull(result);

            Assert.Contains("Erro", result.ViewData.Keys as ICollection);
            StringAssert.AreEqualIgnoringCase("Ocorreu um erro durante o processamento. Tente novamente mais tarde.", result.ViewData["Erro"] as string);
        }
        public void AoAcessarAPaginaDeListagemDeProdutos_OsProdutosDevemPossuirIdENaoDeveSerNegativo()
        {
            _controller = new HomeController(_business.Object);

            var result = _controller.Index() as ViewResult;

            Assert.NotNull(result); var list = ((ProductList)result.Model).Products;

            Assert.IsNotNull(list);

            Assert.IsNotEmpty(list);

            foreach (var produto in list)
            {
                Assert.IsTrue(produto.ProductId >= 0);
            }
        }
        public void SetUp()
        {
            _livroTDD = new Product
                {
                    ProductId = 1,
                    Name = "TDD desenvolvimento guiado por testes",
                    Author = "Kent Beck",
                    Publishing = "Bookman",
                    Year = 2010,
                    Category = Categories.LiteraturaEstrangeira,
                    Stock = 0,
                    Price = 50.0M,
                    Photo = ""
                };

            _livroRomance = new Product
            {
                ProductId = 2,
                Name = "O Amor",
                Author = "Escritora Romance",
                Publishing = "Bookman",
                Year = 2007,
                Category = Categories.LiteraturaBrasileira,
                Stock = 0,
                Price = 30.0M,
                Photo = ""
            };

            _livroFiccao = new Product
            {
                ProductId = 3,
                Name = "O Senhor Dos Aneis",
                Author = "Tolken J.R.",
                Publishing = "Abril",
                Year = 2005,
                Category = Categories.LiteraturaEstrangeira,
                Stock = 0,
                Price = 100.0M,
                Photo = ""
            };

            _listagemDeProdutosEntity = new List<Product>
                {
                  _livroTDD, _livroRomance, _livroFiccao
                };

            _business = new Mock<IProductBusiness>();
            _business.Setup(x => x.GetActiveProducts()).Returns(_listagemDeProdutosEntity);
            _controller = new HomeController(_business.Object);
        }
        public void QuandoUsuarioFiltarAListaPeloName_OControleDeveRetornarSomenteOsLivrosComNamesCorrepondentes()
        {
            var business = new Mock<IProductBusiness>();
            business.Setup(x => x.GetActiveProducts()).Returns(_listagemDeProdutosEntity);

            _controller = new HomeController(business.Object);

            var result = _controller.Search("TDD", null);

            var lista = ((ProductList)result.Model).Products;

            Assert.IsInstanceOf<ProductList>(result.Model);
            StringAssert.AreEqualIgnoringCase(lista[0].Name, _livroTDD.Name);
            Assert.AreEqual(lista.Count, 1);
        }
        public void QuandoUsuarioFiltarAListaComParametrosVazio_OControleDeveRetornarTodosOsLivros()
        {
            var business = new Mock<IProductBusiness>();
            business.Setup(x => x.GetActiveProducts()).Returns(_listagemDeProdutosEntity);

            _controller = new HomeController(business.Object);

            var result = _controller.Search("", null);

            var lista = ((ProductList)result.Model).Products;

            Assert.IsInstanceOf<ProductList>(result.Model);
            StringAssert.AreEqualIgnoringCase(lista[0].Name, _livroTDD.Name);
            StringAssert.AreEqualIgnoringCase(lista[1].Name, _livroRomance.Name);
            StringAssert.AreEqualIgnoringCase(lista[2].Name, _livroFiccao.Name);
            Assert.AreEqual(lista.Count, 3);
        }
        public void AoAcessarAPaginaDeListagem_OsProdutosDevemVirDaCamadaDeNegocios()
        {
            var business = new Mock<IProductBusiness>();
            business.Setup(x => x.GetActiveProducts()).Returns(_listagemDeProdutosEntity);

            _controller = new HomeController(business.Object);

            _controller.Index();

            business.Verify(x => x.GetActiveProducts(), Times.AtLeastOnce());
        }
        public void AoAcessarAPaginaDeListagem_OsProdutosDevemPossuirYearEDeveSerMaiorDoQueZero()
        {
            _controller = new HomeController(_business.Object);

            var result = _controller.Index() as ViewResult;

            Assert.NotNull(result);

            var list = ((ProductList)result.Model).Products;

            Assert.IsNotNull(list);

            Assert.IsNotEmpty(list);

            foreach (var produto in list)
            {
                Assert.IsTrue(produto.Year > 0);
            }
        }
        public void AoAcessarAPaginaDeListagem_APaginaDevePossuirAListagemDeProdutos()
        {
            _controller = new HomeController(_business.Object);

            var result = _controller.Index() as ViewResult;

            Assert.NotNull(result);
            Assert.IsInstanceOf<ProductList>(result.Model);
        }