0

I am trying to create a test case where I need to set an attribute on the mocked object. I've been following the answer in this question but the attribute is null in the CommandEventProcessingDecorator method.

[Fact]
public void RaiseEvent_AfterCommandIsExecuted_WhenCommandIsAttributedWithRaiseEventEnabled()
{
    var command = new FakeCommand();
    Assert.IsAssignableFrom<ICommand>(command);

    var eventProcessor = new Mock<IProcessEvents>(MockBehavior.Strict);
    eventProcessor.Setup(x => x.Raise(command));

    var attribute = new RaiseEventAttribute
    {
        Enabled = true
    };

    var decorated = new Mock<IHandleCommand<FakeCommand>>(MockBehavior.Strict);
    TypeDescriptor.AddAttributes(decorated.Object, attribute); // Add the attribute
    decorated.Setup(x => x.Handle(command));

    var decorator = new CommandEventProcessingDecorator<FakeCommand>(eventProcessor.Object, () => decorated.Object);
    decorator.Handle(command);

    decorated.Verify(x => x.Handle(command), Times.Once);
    eventProcessor.Verify(x => x.Raise(command), Times.Once);
}

internal sealed class CommandEventProcessingDecorator<TCommand> : IHandleCommand<TCommand> where TCommand : ICommand
{
    private readonly IProcessEvents _events;
    private readonly Func<IHandleCommand<TCommand>> _handlerFactory;

    public CommandEventProcessingDecorator(IProcessEvents events, Func<IHandleCommand<TCommand>> handlerFactory)
    {
        _events = events;
        _handlerFactory = handlerFactory;
    }

    public void Handle(TCommand command)
    {
        var handler = _handlerFactory();
        handler.Handle(command);

        var attribute = handler.GetType().GetCustomAttribute<RaiseEventAttribute>(); // attribute
        if (attribute != null && attribute.Enabled)
        {
            _events.Raise(command);
        }
    }
}

What am I doing wrong?

Community
  • 1
  • 1
janhartmann
  • 14,713
  • 15
  • 82
  • 138
  • 1
    From the original answer: `Be aware that Attribute.GetCustomAttributes will not find attributes added at runtime in this way. Instead, use TypeDescriptor.GetAttributes.` – Old Fox Nov 10 '15 at 08:23

1 Answers1

0

This was actually answered in the question referenced.

As Old Fox pointed out in a comment to the question: Be aware that Attribute.GetCustomAttributes will not find attributes added at runtime in this way. Instead, use TypeDescriptor.GetAttributes.

I created a extension method for getting the Attribute:

/// <summary>
/// Gets a runtime added attribute to a type.
/// </summary>
/// <typeparam name="TAttribute">The attribute</typeparam>
/// <param name="type">The type.</param>
/// <returns>The first attribute or null if none is found.</returns>
public static TAttribute GetRuntimeAddedAttribute<TAttribute>(this Type type) where TAttribute : Attribute
{
    if (type == null) throw new ArgumentNullException(nameof(type));

    var attributes = TypeDescriptor.GetAttributes(type).OfType<TAttribute>();
    var enumerable = attributes as TAttribute[] ?? attributes.ToArray();
    return enumerable.Any() ? enumerable.First() : null;
}

Usage: myClass.getType().GetRuntimeAddedAttribute<CustomAttribute>();

Community
  • 1
  • 1
janhartmann
  • 14,713
  • 15
  • 82
  • 138