如何防止 AutoFixture 的 GuardClauseAssertion 检查继承的 class 中的构造函数?
How do I prevent AutoFixture's GuardClauseAssertion from checking the constructor in an inherited class?
我有一个 class 继承自 Semantic Type project 中的 SemanticType class:
我正在尝试测试 my class 并确保我有一个保护子句防止传递 Null 和空字符串。但是,测试失败,因为继承的 class,SemanticType,没有 Guard 子句。我如何哄 AutoFixture 只查看我的 ZipCode class?
中的构造函数
public class ZipCode : SemanticType<string>
{
public ZipCode(string value) : base(IsValid, value)
{
Guard.NotNullOrEmpty(() => value, value);
Guard.IsValid(() => value, value, IsValid, "Invalid Zip Code");
}
}
调试时,测试在 SemanticTypes.dll 中失败并显示以下消息:
An exception of type 'System.ArgumentException' occurred in SemanticTypes.dll but was not handled in user code
也就是说,SemanticType的构造函数中没有Guard子句。如果您能提供任何帮助,我们将不胜感激。
您可以使用 GuardClauseAssertion
的 Verify
的重载之一,它将 ConstructorInfo 作为参数。
然后您可以通过仅选择属于 ZipCode
class:
的那些构造函数来进一步限制断言
[Test, AutoData]
public void SutConstructorHasAppropriateGuardClauses(
GuardClauseAssertion assertion)
{
assertion.Verify(
typeof(ZipCode).GetConstructors(BindingFlags.Public));
}
虽然我没有访问 SemanticType 项目的权限,但上面的测试应该正常通过。
我有一个 class 继承自 Semantic Type project 中的 SemanticType class:
我正在尝试测试 my class 并确保我有一个保护子句防止传递 Null 和空字符串。但是,测试失败,因为继承的 class,SemanticType,没有 Guard 子句。我如何哄 AutoFixture 只查看我的 ZipCode class?
中的构造函数public class ZipCode : SemanticType<string>
{
public ZipCode(string value) : base(IsValid, value)
{
Guard.NotNullOrEmpty(() => value, value);
Guard.IsValid(() => value, value, IsValid, "Invalid Zip Code");
}
}
调试时,测试在 SemanticTypes.dll 中失败并显示以下消息:
An exception of type 'System.ArgumentException' occurred in SemanticTypes.dll but was not handled in user code
也就是说,SemanticType的构造函数中没有Guard子句。如果您能提供任何帮助,我们将不胜感激。
您可以使用 GuardClauseAssertion
的 Verify
的重载之一,它将 ConstructorInfo 作为参数。
然后您可以通过仅选择属于 ZipCode
class:
[Test, AutoData]
public void SutConstructorHasAppropriateGuardClauses(
GuardClauseAssertion assertion)
{
assertion.Verify(
typeof(ZipCode).GetConstructors(BindingFlags.Public));
}
虽然我没有访问 SemanticType 项目的权限,但上面的测试应该正常通过。