BDD para C # NUnit
He estado usando una extensión de BDD Spec elaborada en casa para escribir pruebas de estilo BDD en NUnit, y quería ver qué pensaban todos. ¿Agrega valor? ¿Es una mierda? Si es así, ¿por qué? ¿Hay algo mejor por ahí?
Aquí está la fuente:https://github.com/mjezzi/NSpec
Hay dos razones por las que creé esto
Para que mis exámenes sean fáciles de leer.Para producir una salida en inglés simple para revisar las especificaciones.Aquí hay un ejemplo de cómo se verá una prueba:
-desde que los zombis parecen ser populares en estos días ...
Dado un Zombie, Peson e IWeapon:
namespace Project.Tests.PersonVsZombie
{
public class Zombie
{
}
public interface IWeapon
{
void UseAgainst( Zombie zombie );
}
public class Person
{
private IWeapon _weapon;
public bool IsStillAlive { get; set; }
public Person( IWeapon weapon )
{
IsStillAlive = true;
_weapon = weapon;
}
public void Attack( Zombie zombie )
{
if( _weapon != null )
_weapon.UseAgainst( zombie );
else
IsStillAlive = false;
}
}
}
Y las pruebas de estilo NSpec:
public class PersonAttacksZombieTests
{
[Test]
public void When_a_person_with_a_weapon_attacks_a_zombie()
{
var zombie = new Zombie();
var weaponMock = new Mock<IWeapon>();
var person = new Person( weaponMock.Object );
person.Attack( zombie );
"It should use the weapon against the zombie".ProveBy( spec =>
weaponMock.Verify( x => x.UseAgainst( zombie ), spec ) );
"It should keep the person alive".ProveBy( spec =>
Assert.That( person.IsStillAlive, Is.True, spec ) );
}
[Test]
public void When_a_person_without_a_weapon_attacks_a_zombie()
{
var zombie = new Zombie();
var person = new Person( null );
person.Attack( zombie );
"It should cause the person to die".ProveBy( spec =>
Assert.That( person.IsStillAlive, Is.False, spec ) );
}
}
Obtendrá la salida de especificaciones en la ventana de salida:
[PersonVsZombie]
- PersonAttacksZombieTests
When a person with a weapon attacks a zombie
It should use the weapon against the zombie
It should keep the person alive
When a person without a weapon attacks a zombie
It should cause the person to die
2 passed, 0 failed, 0 skipped, took 0.39 seconds (NUnit 2.5.5).