Rafael Noronha

closing the gap between business and technology

Teste dedo-duro

with one comment

Tem um colega de equipe que não dá pra confiar?
creisson
Seus problemas acabaram…

Teste dedo-duro


        [Test]
        public void controllers_wich_require_full_authentication()
        {
            var theseDoesNotRequire = new[] {
                typeof(AccountController),
                typeof(ErrorController),
                typeof(CustomController),
                typeof(HomeController)
            };

            foreach (var c in typeof(CustomController).Assembly.GetTypes()
                .Where(t => typeof(Controller).IsAssignableFrom(t)
                    && !theseDoesNotRequire.Contains(t)))
            {
                var attr = c.GetCustomAttributes(typeof(AuthorizeAttribute), false);
                Assert.IsTrue(attr.Length == 1);

                var authorize = attr[0] as AuthorizeAttribute;
                Assert.AreEqual(string.Empty, authorize.Roles);
                Assert.AreEqual(string.Empty, authorize.Users);
                Assert.AreEqual(-1, authorize.Order);
            }
        }

        [Test]
        public void all_controllers_should_inherit_custom_controller()
        {
            foreach (var c in typeof(CustomController).Assembly.GetTypes()
                .Where(t => typeof(Controller).IsAssignableFrom(t)
                    && t != typeof(CustomController)))
            {
                Assert.AreEqual(typeof(CustomController), c.BaseType);
            }
        }

        [Test]
        public void all_persisted_models_must_inherit_this_class()
        {
            foreach (Type type in typeof(WorkPeriod).Assembly.GetTypes()
                .Where(t => t.GetCustomAttributes(typeof(ActiveRecordAttribute), false)
                    .Count() > 0))
            {
                Assert.IsNotNull(type.BaseType);
                Assert.AreEqual(typeof(IdentifiedEntity<>), type.BaseType.GetGenericTypeDefinition());
            }
        }

Obs:

Provavelmente uma ferramenta legal para tratar este tipo de problema é um code analyser, como o FxCop.
Mas esta não deixa de ser uma opção bastante prática.

Sobre o colega incompetente, um pé na bunda é sempre a melhor solução (quando se trata daquele profissional que é ruim e não quer melhorar).

Independente da competência da equipe, é sempre bom automatizar este tipo de coisa!


Help me to spread the word!
  • Digg
  • del.icio.us
  • RSS
  • DotNetKicks
  • Technorati
  • Twitter
  • FriendFeed

Related posts:

  1. (Inglês) Database Integration Tests – implementing it
  2. (Inglês) Model validation specifications
  3. Testando a engine de blog com BDD

Written by rafanoronha

July 14th, 2009 at 1:57 am

One Response to 'Teste dedo-duro'

Subscribe to comments with RSS or TrackBack to 'Teste dedo-duro'.

  1. Rafael,

    Interessante a idéia!!!
    Bem legal…

    []´s

    Juliano Oliveira

    14 Jul 09 at 8:37

Leave a Reply