Update: I've just realised that you can actually add attributes to an existing type by using TypeDescriptor.AddAttributes, which can be executed against an instance or a a type:
Mock<IRepository> repositoryMock = new Mock<IRepository>();
CustomAttribute attribute = new CustomAttribute();
// option #1: to the instance
TypeDescriptor.AddAttributes(repositoryMock.Object, attribute );
// option #2: to the generated type
TypeDescriptor.AddAttributes(repositoryMock.Object.GetType(), attributes);
If you need it, AddAttribute returns a TypeDescriptorProvider that can be passed to TypeDescriptor.RemoveProvider to remove the attributes afterwards.
Original Answer
I don't belive Moq (or any other mocking framework for that matter) supports custom attributes. I do know that Castle Proxy (the framework commonly used to actually create the class) does support it, but there'd be no way to access it through Moq.
Your best bet is to abstract your method of loading Attributes into an interface (that accepts the Type and the Attribute type) and then mock that.
Edit: For example:
public interface IAttributeStrategy
{
Attribute[] GetAttributes(Type owner, Type attributeType, bool inherit);
Attribute[] GetAttributes(Type owner, bool inherit);
}
public class DefaultAttributeStrategy : IAttributeStrategy
{
public Attribute[] GetAttributes(Type owner, Type attributeType, bool inherit)
{
return owner.GetCustomAttributes(attributeType, inherit);
}
public Attribute[] GetAttributes(Type owner, bool inherit)
{
return owner.GetCustomAttributes(inherit);
}
}
The class that needs the attributes uses an instance of IAttributeStrategy (either through an IoC container, or having it optionally passed into the constructor). Usually it will be a DefaultAttributeStrategy, but you can now mock IAttributeStrategy in order to override the output.
It might sound convoluted, but adding a layer of abstraction is much easier than attempting to trying to actually mock Attributes.