Idioms
Over time developers have agreed on certain best practices. Writing tests to ensure those best practices are followed through can be tedious.
Based on AutoFixture, the classes contained in the package
AutoFixture.Idioms
help creating unit tests that quickly check if these best practices are properly followed.The
AutoFixture.Idioms
package contains several assertions. Each assertion encapsulates a unit test that tests a specific expectation on the system under test.All assertions implement the interface
IIdiomaticAssertion
. This interface exposes a plethora of overloads of the Verify
methods accepting everything from a set of assemblies down to a single member.The abstract class
IdiomaticAssertion
offers a basic implementation of all overloads but the ones at the end of the tree (constructors, methods, properties, fields).Given how the
IdiomaticAssertion
class works, developers can target a specific member or the whole type. It will be up to the author of the assertion to make sure that only the suitable members are tested.var fixture = new Fixture();
var assertion = fixture.Create<MyFakeAssertion>();
assertion.Verify(typeof(TestClass));
assertion.Verify(typeof(TestClass).GetMethods());
assertion.Verify(typeof(TestClass).GetMethod(nameof(TestClass.TestMethod)));
Here is a list of scenarios that can be accellerated by using assertions available in the
AutoFixture.Idioms
package.When writing methods or constructors, it is good practice to protect them against unsupported null values. The
GuardClauseAssertion
verifies that the parameters passed to a method or a constructor are properly checked against null values.Assuming a test class like the following one (for semplicity we will use
string
as dependencies):public class TestClass
{
private readonly string _firstDependency;
private readonly string _secondDependency;
public TestClass(string firstDependency, string secondDependency)
{
_firstDependency = firstDependency ?? throw new ArgumentNullException(nameof(firstDependency));
_secondDependency = secondDependency ?? throw new ArgumentNullException(nameof(secondDependency));
}
public void DoSomething(string parameter)
{
if (parameter == null) throw new ArgumentNullException(nameof(parameter));
...
}
}
The snippet below will test that every parameter of the constructor is guarded against null values.
[Test]
public void Constructor_is_guarded_against_nulls()
{
// ARRANGE
var fixture = new Fixture();
var assertion = fixture.Create<GuardClauseAssertion>();
// ACT & ASSERT
assertion.Verify(typeof(TestClass).GetConstructors());
}
If any of the nullable parameters of the constructor were not to be guarded, an exception would be thrown and the unit test would fail.
To be noted that without the help of the Idioms package, the developer would expected to write
N+1
unit tests, only for the constructor: one for each incoming parameter and one for the correct initialization of the system under test.[Test]
public void FirstDependency_is_required()
{
// ARRANGE
var fixture = new Fixture();
// ACT & ASSERT
Assert.That(() => new TestClass(null, fixture.Create<string>()), Throws.ArgumentNullException);
}
[Test]
public void SecondDependency_is_required()
{
// ARRANGE
var fixture = new Fixture();