jeudi 25 juin 2015

NUnit - Getter Setter Method Testing

I am trying to test out a getter and setter method as follows

  public class GetToken
     {
         public string TokenStatusCode { get; set; }
         public AccountPrimary TokenKey { get; set; }
     }

with the NUnit code as follows

    [Test]
    public void GetToken_StatusCode()
    {
        TestHelperGetterSetter<GetToken, string>(new StackFrame().GetMethod(), 
        "TokenStatusCode", "RipSnorter");
    }

    [Test]
    public void GetToken_TokenIden()
    {
        TestHelperGetterSetter<GetToken, object>(new StackFrame().GetMethod(),
        "TokenKey", 77);
    }

With a helper as follows

  private void TestHelperGetterSetter<TAttr, TProp>(MethodBase method,
                 string argName, TProp expectedValue)
    {
        object[] customAttributes = method.GetCustomAttributes(typeof(TAttr), false);

        Assert.AreEqual(1, customAttributes.Count());

        TAttr attr = (TAttr)customAttributes[0];

        PropertyInfo propertyInfo = attr.GetType().GetProperty(argName);

        Assert.IsNotNull(propertyInfo);
        Assert.AreEqual(typeof(TProp), propertyInfo.PropertyType);
        Assert.IsTrue(propertyInfo.CanRead);
        Assert.IsTrue(propertyInfo.CanWrite);
        Assert.AreEqual(expectedValue, (TProp)propertyInfo.GetValue(attr, null));
    }

Everytime I run the test the test fails with the result as below

 Expected: 1
 But was:  0

Can someone let me know, what am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire