TortillaCurtain
TortillaCurtain

Reputation: 531

How do I prevent AutoFixture's GuardClauseAssertion from checking the constructor in an inherited class?

I have a class that inherits from the SemanticType class in the Semantic Type project:

I am trying to test my class and ensure that I have a guard clause preventing Null and empty strings from being passed. However, the test fails because the inherited class, SemanticType, does not have a Guard clause. How do I coax AutoFixture to only look at the constructor in my 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");
     }
 }

When debugging, the test is failing in SemanticTypes.dll with this message:

An exception of type 'System.ArgumentException' occurred in SemanticTypes.dll but was not handled in user code

In other words, there's no Guard clause in the constructor in SemanticType. Any help you can provide would be most appreciated.

Upvotes: 4

Views: 1076

Answers (1)

Nikos Baxevanis
Nikos Baxevanis

Reputation: 11211

You can use one of the overloads of GuardClauseAssertion's Verify that takes a ConstructorInfo as a paramerer.

Then you can further constrain the assertion, by selecting only those constructors belonging into ZipCode class:

[Test, AutoData]
public void SutConstructorHasAppropriateGuardClauses(
    GuardClauseAssertion assertion)
{
    assertion.Verify(
        typeof(ZipCode).GetConstructors(BindingFlags.Public));
}

Although I don't have access to SemanticType project, the above test should, normally, pass.

Upvotes: 4

Related Questions