21/8/2010 13:22 Lucas Ontivero

Cuán cortas y cuan claras deben ser las pruebas unitarias?

Las pruebas unitarias debería ser así de cortas y claras:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Losoft.Temo.Security.Authorization.Exceptions;
 
namespace Losoft.Temo.Security.Authorization.Tests
{
    [TestFixture]
    public class AdminGroupSpecs : TestsWithSecurable
    {
        [TestCase("R"), TestCase("W"), TestCase("V"), TestCase("A")]
        public void Will_Allow_All_Operation_To_Admin_Accounts(string rightToVerify)
        {           
            
            Assert.That(admin.HasRight(rightToVerify, resource));
        }
 
        [Test]
        public void Should_Be_Able_To_Grant_Rights_To_Other_Accounts()
        {
            admin.Grant(right: "A", securableResource: resource, toAccount: simpleUser);
            Assert.That(simpleUser.HasRight("A", resource));
        }
 
        [Test]
        public void Should_Be_Able_To_Revoke_Rights_To_Other_Accounts()
        {
            admin.Grant(right: "W", securableResource: resource, toAccount: simpleUser);
            admin.Revoke(right: "W", securableResource: resource, toAccount: simpleUser);
            Assert.That(!resource.HasRight(simpleUser, "W", admin));
        }
 
        [Test]
        public void Should_Not_Be_Able_To_Revoke_Administrative_Right()
        {
            admin.Grant(right: "A", securableResource: resource, toAccount: simpleUser);
            Assert.That(() => simpleUser.Revoke(right: "A", securableResource: resource),
                Throws.TypeOf<SecurityException>().With.Message.EqualTo("Unable to auto revoke administrative rights."));
            Assert.That(simpleUser.HasRight("A", resource));
        }
 
        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
        }
    }
}

Un poco más:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using Losoft.Temo.Common;
using Losoft.Temo.Security.Authorization.Dependency;
 
namespace Losoft.Temo.Security.Authorization.Tests
{
    [TestFixture]
    public class LogChangesSpecs : TestsWithSecurable 
    {
        [Test]
        public void Will_Log_Fault_Attempts_To_Grant_Rights()
        {
            Assert.Throws(new AssignableToConstraint(typeof(Exception)), ()=>
            simpleUser.Grant(right: "A", securableResource: resource));
 
            Assert.That(logger.LoggedMessage, Is.EqualTo(
                "Info: 30/03/2010 03:00:00 a.m. failed attempt by simple-user to grant [A] right to simple-user."));  
        }
 
        [Test]
        public void Will_Log_Fault_Attempts_To_Revoke_Rights()
        {
            Assert.Throws(new AssignableToConstraint(typeof(Exception)), () =>
            simpleUser.Revoke(right: "A", securableResource: resource));
 
            Assert.That(logger.LoggedMessage, Is.EqualTo(
                "Info: 30/03/2010 03:00:00 a.m. failed attempt by simple-user to revoke [A] right to simple-user."));
        }
 
        [Test]
        public void Will_Log_Fault_Attempts_To_Query_Rights()
        {
            Assert.Throws(new AssignableToConstraint(typeof(Exception)), () =>
            simpleUser.HasRight(right: "A", securableResource: resource, toAccount: admin));
 
            Assert.That(logger.LoggedMessage, Is.EqualTo(
                "Info: 30/03/2010 03:00:00 a.m. failed attempt by simple-user to query [A] right to admin-user."));
        }
 
        [Test]
        public void Will_Log_Grant_Rights()
        {
            admin.Grant(right: "A", securableResource: resource, toAccount: simpleUser);
 
            Assert.That(logger.LoggedMessage, Is.EqualTo(
                "Audit: 30/03/2010 03:00:00 a.m. admin-user has granted [A] right to simple-user."));
        }
 
        [Test]
        public void Will_Log_Revoke_Rights()
        {
            admin.Revoke(right: "A", securableResource: resource, toAccount: simpleUser);
 
            Assert.That(logger.LoggedMessage, Is.EqualTo(
                "Audit: 30/03/2010 03:00:00 a.m. admin-user has revoked [A] right to simple-user."));
        }
 
        [Test]
        public void Will_Log_Revoke_Rights()
        {
            admin.Revoke(right: "A", securableResource: resource, toAccount: simpleUser);
 
            Assert.That(logger.LoggedMessage, Is.EqualTo(
                "Audit: 30/03/2010 03:00:00 a.m. admin-user has revoked [A] right to simple-user."));
        }
 
        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
 
            logger = new FakeLogger();
            LoggerDependency.Instance = () => logger;
 
            DateTimeDependency.Now = () => new DateTime(2010, 3, 30);
        }
 
        private FakeLogger logger;
    }
}
Archivado en: ,,,,,,,
Comparte este post: