我正在使用 Ben Morris 的 NetArchTest.Rules 包来实施分层架构。在我的代码中,我检查资产项目是否未引用访问层中任何其他项目的任何其他项目(具有依赖关系),但结果始终返回 false,无论代码是否应该或不应该。我确信资产项目与在测试 AssetsShouldNotReferenceOtherAccess 中检查的项目没有任何项目依赖关系。
private const string AssetsNamespace = "Flux.Access.Assets";
private const string BlobNamespace = "Flux.Access.Blob";
private const string ConfigurationsNamespace = "Flux.Access.Configurations";
private const string MembersNamespace = "Flux.Access.Members";
private const string CommonNamespace = "Flux.Access.Common";
[Test]
public void AssetsShouldNotReferenceOtherAccess()
{
var otherProjects = new[]
{
BlobNamespace, ConfigurationsNamespace, MembersNamespace
};
var result = Types
.InCurrentDomain()
.That()
.ResideInNamespace(AssetsNamespace)
.ShouldNot()
.HaveDependencyOnAny(otherProjects)
.GetResult()
.IsSuccessful;
Assert.True(result);
}
[Test]
public void AssetsShouldReferenceCommon()
{
var result = Types
.InCurrentDomain()
.That()
.ResideInNamespace(AssetsNamespace)
.Should()
.HaveDependencyOn(CommonNamespace)
.GetResult()
.IsSuccessful;
Assert.True(result);
}
如果我将代码更改为 Should 而不是 ShouldNot,我预计 AssetsShouldNotReferenceOtherAccess 中的结果会发生变化,但是在这两种情况下代码都返回 False。在我在线阅读的软件包文档中,这似乎是测试代码的正确方法,但我无法让它工作。任何见解都会有帮助:)
你可以使用类似的东西
var result = Types.InAssembly(assembly)
.Should()
.NotHaveDependencyOnAll(notDependOnList)
.GetResult();