2

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?

3

3 Answers 3

1

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);

    }
}
2
  • But can I attach this attribute on a mock, so that I don't have to create placeholders? see my example.
    – bob
    Nov 26, 2008 at 10:31
  • 1
    Ahhhh my eyes! For <insert deity>'s sake, move those attributes above the class / method declaration. May 4, 2010 at 20:06
1

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.

1
  • 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
    Mar 20, 2009 at 11:32
0

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.

1
  • 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
    May 5, 2010 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.