views:

49

answers:

3

I'm writing a unit test to verify that the serializable attributes are set on my class. I thought I was on the right way, but for some reason I can't find the attribute types.

Here is my class:

[DataContract]
public class User
{
    [DataMember]
    public int Id { get; set; }
}

And a unit test for checking the attributes:

[Test]
public void DataMembersSetAsExpected()
{
    var type = typeof(User);
    Assert.That(type.IsDefined(typeof(System.Runtime.Serialization.DataContractAttribute), true));
    var idProperty = type.GetProperty("Id");
    Assert.That(idProperty.IsDefined(typeof(System.Runtime.Serialization.DataMemberAttribute), true));
}

The problem here is that the types of the attributes are unknown. Where can I find the right attribute definitions?

System.Runtime.Serialization.DataContractAttribute
System.Runtime.Serialization.DataMemberAttribute
+1  A: 

Add a reference to System.Runtime.Serialization.dll.

Jakob Christensen
Now, why didn't ReSharper tell me this like it normally does :-) You're of course correct - and you were first. Thanx!
stiank81
+1  A: 

You need to reference the System.Runtime.Serialization assembly in your unit test project.

Darin Dimitrov
+1  A: 

I have a Fixture class that I use for unit testing (test data generator) and I have made these extension methods for it:

    public static void SutPropertyHasAttribute<TSut, TProperty>(this Fixture fixture, Expression<Func<TSut, TProperty>> propertyExpression, Type attributeType)
    {
        var pi = (PropertyInfo)((MemberExpression)propertyExpression.Body).Member;
        var count = pi.GetCustomAttributes(attributeType, true).Count();
        Assert.AreEqual(1, count);
    }

    public static void SutHasAttribute<TSut>(this Fixture fixture, Type attributeType)
    {
        var type = typeof(TSut);
        var count = type.GetCustomAttributes(attributeType, true).Count();
        Assert.AreEqual(1, count);
    }

    public static void SutMethodHasAttribute<TSut>(this Fixture fixture, Expression<Action<TSut>> methodExpression, Type attributeType)
    {
        var mi = (MethodInfo)((MethodCallExpression)methodExpression.Body).Method;
        var count = mi.GetCustomAttributes(attributeType, true).Count();
        Assert.AreEqual(1, count);
    }

Now I call it like this from my tests:

    [TestMethod]
    public void SutHasDataContractAttribute()
    {
        // Fixture setup
        // Exercise system and verify outcome
        new Fixture().SutHasAttribute<Flag>(typeof(DataContractAttribute));
        // Teardown
    }

    [TestMethod]
    public void FlagGroupIdHasDataMemberAttribute()
    {
        // Fixture setup
        // Exercise system and verify outcome
        new Fixture().SutPropertyHasAttribute((Flag f) => f.FlagGroupId, typeof(DataMemberAttribute));
        // Teardown
    }

The Flag class looks like this:

[DataContract(Namespace ="http://mynamespace")]
public class Flag
{
    [DataMember]
    public string FlagGroupId { get; set; }
}

Of course you need a reference to System.Runtime.Serialization like this:

using System.Runtime.Serialization;
klausbyskov