BenMorris / NetArchTest

A fluent API for .Net that can enforce architectural rules in unit tests.
MIT License
1.41k stars 83 forks source link

FailingTypes is empty when ConditionLists are aggregated by And() #128

Closed mr-cf closed 1 year ago

mr-cf commented 1 year ago

Hello,

I'm using following code the test that the Type should meet two conditions:

.Should().BeInterfaces().And().NotBeGeneric().GetResult();

I test following types:

public class BreaksShouldBeInterface { }
public interface BreaksShouldNotBeGeneric<T> {}

The first type breaks BeInterface() rule. The second type breaks NotBeGeneric() rule.

When conditions are concatenated by And() function, then FailingTypes collection is empty. In my opinion FailingTypes should contain both of the types.

I would like to use FailingTypes to fail with meaningful message explaining which of the types caused the failure.

Here is the full source to reproduce this issue:

#pragma warning disable CA1034, SA1302, SA1201
    [TestFixture]
    public class ArchitectureTests
    {
        [Test]
        public void Logical_ops_check()
        {
            // Should() comes from FluentAssertions lib.
            var result1 = Types.InAssembly(typeof(ArchitectureTests).Assembly).That().HaveNameStartingWith("BreaksShould")
                .Should()
                .BeInterfaces()
                .GetResult();

            result1.FailingTypes.Should().HaveCount(1).And.Contain(typeof(BreaksShouldBeInterface));

            var result2 = Types.InAssembly(typeof(ArchitectureTests).Assembly).That().HaveNameStartingWith("BreaksShould")
                .Should()
                .NotBeGeneric()
                .GetResult();

            result2.FailingTypes.Should().HaveCount(1).And.Contain(typeof(BreaksShouldNotBeGeneric<>));

            var result3 = Types.InAssembly(typeof(ArchitectureTests).Assembly).That().HaveNameStartingWith("BreaksShould")
                .Should()
                .BeInterfaces().And().NotBeGeneric()
                .GetResult();

            // Expecting FailingTypes to contain:
            // - BreaksShouldBeInterface
            // - BreaksShouldNotBeGeneric<T>,
            // but is empty.
            result3.FailingTypes.Should().HaveCount(2);
        }

        public class BreaksShouldBeInterface
        {
        }

        public interface BreaksShouldNotBeGeneric<T>
        {
        }
    }
NeVeSpl commented 1 year ago

I am not able to reproduce that problem:

image

mr-cf commented 1 year ago

I'm apologise. I retried and now the test case works fine and I also cannot reproduce the issue.

I wonder what circumstances could produce such an error.

Best regards, MR