Vinicius Saeta
Vinicius Saeta

Reputation: 183

It is possible to mock custom attributes c#?

It is possible to mock custom attributes c#?

For example:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] 
public class YourAttribute : Attribute
{
}

[Your]
public void MyMethod()
{
   //something
}

I want to test the MyMethod, but I need to simulate the attribute Your.

Upvotes: 2

Views: 1200

Answers (1)

Allan Elder
Allan Elder

Reputation: 4094

Yes, you can add an attribute to an instance or to a type via TypeDescriptor; following example uses Moq, but it can be applied elsewhere.

var mock = new Mock<IMyInterface>();

var attribute = new YourAttribute();

TypeDescriptor.AddAttributes(mock.Object, attribute);

Upvotes: 1

Related Questions