So far I’ve never used the ‘using static’ directive introduced in C# 6. To simplify the assertions of my tests I created a static TestHelper:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class TestHelper | |
{ | |
public static Assembly ApplicationAssembly = typeof(ApplicationModule).Assembly; | |
public static Assembly DomainAssembly = typeof(Order).Assembly; | |
public static Assembly InfrastructureAssembly = typeof(InfrastructureModule).Assembly; | |
public static void AssertFailingTypes(IEnumerable<Type> types) | |
{ | |
Assert.True(types == null || types.Count() == 0); | |
} | |
public static void AssertArchTestResult(TestResult result) | |
{ | |
AssertFailingTypes(result.FailingTypes); | |
} | |
} |
I’m using this TestHelper to simplify my NetArchTests(but more about that in another blog post).
Inside my tests I can now do the following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Xunit; | |
using static eShopExample.ArchitectureTests.TestHelper; | |
namespace eShopExample.ArchitectureTests | |
{ | |
public class LayersTests | |
{ | |
[Fact] | |
public void DomainLayer_DoesNotHaveDependency_ToApplicationLayer() | |
{ | |
var result = Types.InAssembly(DomainAssembly) | |
.Should() | |
.NotHaveDependencyOn(ApplicationAssembly.GetName().Name) | |
.GetResult(); | |
AssertArchTestResult(result); | |
} | |
} | |
} |
Neat!