views:

674

answers:

3

I use custom Attributes in a project and I would like to integrate them in my unit-tests.

Now I use Rhino Mocks to create my mocks but I don't see a way to add my attributes (and there parameters) to them.

Did I miss something, or is it not possible? Other mocking framework? Or do I have to create dummy implementations with my attributes?

example: I have an interface in a plugin-architecture (IPlugin) and there is an attribute to add meta info to a property. Then I look for properties with this attribute in the plugin implementation for extra processing (storing its value, mark as gui read-only...)

Now when I create a mock can I add easily an attribute to a property or the object instance itself?

EDIT: I found a post with the same question -> link. The answer there is not 100% and it is Java...

EDIT 2: It can be done... searched some more (on SO) and found 2 related questions (+ answers)

here and here

Now, is this already implemented in one or another mocking framework?

+1  A: 

Here's an example of how I've tested custom attributes. In this case I've got a custom attribute that's used on a method. In my test class, I've created a placeholder method to which the custom attribute is applied. I've then used reflection to get the instance of the custom attribute.

[TestFixture] public class SomeRandomAttributeTest
{
 [SomeRandom(RestrictionType.Local)]
 public void PlaceholderMethodForAttribute() {throw new ApplicationException(this.ToString());}

 [Test]public void BlahBlahIsBlahTheBlah()
 {
  object[] attributes = this.GetType().GetMethod("PlaceholderMethodForAttribute").GetCustomAttributes(false);
  Assert.AreEqual(1, attributes.Length);
  Assert.IsInstanceOfType(typeof(SomeRandomAttribute), attributes[0]);

  Assert.AreEqual("Yada yada yada", ((SomeRandomAttribute) attributes[0]).Yada);

 }
}
Joseph Anderson
But can I attach this attribute on a mock, so that I don't have to create placeholders? see my example.
bob
Ahhhh my eyes! For <insert deity>'s sake, move those attributes above the class / method declaration.
Chris Shouts
+1  A: 

Why can't you just do something like:

var sut = new SomeRandomAttribute(RestrictionType.Local);
Assert.AreEqual("Yada yada yada", sut.Yada);

If you would like to test the code that visits all your types and does some stuff for each of the SomeRandomAttribute's it finds, I would suggest you split it in retrieving the MethodInfo objects that are decorated with your custom attribute and the operations that need to be performed on these objects. The later should be easy to test given a correctly configured MethodInfo object and attribute. The former could be done by using specific test placeholders for your attribute like suggested by Joseph.

Bas Bossink
The problem is that with a lot of attributes there is no logic to test. Sometimes they are only markers. So I look for IPlugin implementations in an assembly, create an instance and store some metadata / perform attribute logic. And that I want to test in LoadPluginTest(IPlugin mock) with a mock.
bob
A: 

It depends on what you need to test:

  • Testing some "functionality" of the attribute, if the attribute you've added has some effect on the class/method it decorates you can create a dummy class that inherit IPlugin with that attribute and test that its behavior was affected from the attribute presence or value.
  • If you want to test some inner functionality of the attribute then just create that attribute and use assert to verify that end result.

I don't think you need mocking here.

Dror Helper
I want indeed test the functionality, not inner logic but without the hassle of creating dummy classes (what I do now), so using a mocking framework. But there is no other option I think... thanks...
bob