Testing Arguments of Attributes

Nov 13, 2011 at 10:31 PM

Given:

    [ContentType("output")]
    [Export(typeof(IClassifierProvider))]
    public class OutputClassifierProvider : IClassifierProvider
    {
    }

I can write the following test:

        [Test]
        public void GetClassifierAttributes()
        {
            typeof(OutputClassifierProvider).Should().BeDecoratedWith<ContentTypeAttribute>();
            typeof(OutputClassifierProvider).Should().BeDecoratedWith<ExportAttribute>();
        }
Which is stunningly cool. Is there a way to assert the arguments of the attributes as well?

Nov 18, 2011 at 3:31 PM

Currently it is not possible to assert the arguments of the attributes.

Coordinator
Jan 8, 2012 at 8:16 AM

@blueonion If you can think of a nice syntax, we might consider it.

Jan 8, 2012 at 11:58 AM

The simplest form I can think of is:

typeof(OutputClassifierProvider).Should().BeDecoratedWith<ContentTypeAttribute>("output")

Another form might be:

typeof(OutputClassifierProvider).Should().BeDecoratedWith<ContentTypeAttribute>().WithArg("output")

Or:

typeof(OutputClassifierProvider).Should().BeDecoratedWith<ContentTypeAttribute>().WithArgs(new object[] {"output"})

Where WithArgs takes an object array. I don't write fluent interfaces very often but know they're difficult to get right and implement.

Coordinator
Jan 9, 2012 at 6:30 PM

What you are trying to do is not possible. When you query a type for an attribute, Reflection will return you an instance of the attribute, not its definition. So if you want to make sure the attribute was configured properly, you need to expose that data through properties.

Jun 3, 2012 at 7:45 AM

We have created issue 12186 to support this.
See http://fluentassertions.codeplex.com/workitem/12186.