2017-05-26 32 views
0

請參閱下面的代碼:應用GetProperties,用於接口只

public static class AssertEx 
{ 
    public static void PropertyValuesAreEquals(object actual, object expected) 
    { 
     PropertyInfo[] properties = expected.GetType().GetProperties(); 
     foreach (PropertyInfo property in properties) 
     { 
      object expectedValue = property.GetValue(expected, null); 
      object actualValue = property.GetValue(actual, null); 

      if (actualValue is IList) 
       AssertListsAreEquals(property, (IList)actualValue, (IList)expectedValue); 
      else if (!Equals(expectedValue, actualValue)) 
       Assert.Fail("Property {0}.{1} does not match. Expected: {2} but was: {3}", property.DeclaringType.Name, property.Name, expectedValue, actualValue); 
     } 
    } 

    private static void AssertListsAreEquals(PropertyInfo property, IList actualList, IList expectedList) 
    { 
     if (actualList.Count != expectedList.Count) 
      Assert.Fail("Property {0}.{1} does not match. Expected IList containing {2} elements but was IList containing {3} elements", property.PropertyType.Name, property.Name, expectedList.Count, actualList.Count); 

     for (int i = 0; i < actualList.Count; i++) 
      if (!Equals(actualList[i], expectedList[i])) 
       Assert.Fail("Property {0}.{1} does not match. Expected IList with element {1} equals to {2} but was IList with element {1} equals to {3}", property.PropertyType.Name, property.Name, expectedList[i], actualList[i]); 
    } 
} 

我把這個從這裏:Compare equality between two objects in NUnit(Juanmas的回答)

說我有一個這樣的接口:

public interface IView 
{ 
    decimal ID { get; set; } 
    decimal Name { get; set; } 
} 

然後我有兩個這樣的看法:

IView view = new FormMain(); 
IView view2 = new FormMain(); 

view和view2然後被賦予屬性。

我那麼想的接口,所以我這樣做比較:

Assert.AreEqual(Helper.PropertyValuesAreEquals(view, view2), true); 

然而,這將產生一個例外: 「物業GET方法未找到」

我該如何確保此函數只獲取屬性在我的界面中的屬性?

我是否應該單元測試這樣的視圖?

+0

您可以考慮使用泛型,同樣在上面的邏輯中,您應該檢查屬性是否存在於提供的兩個對象中。 – Nkosi

回答

0

當您只有setter的財產時,會發生Property Get method was not found.錯誤,例如, int MyProperty { set { x = value; } }

因此,您只需跳過PropertyValuesAreEquals方法中的這些屬性即可。

如果你想檢查屬於特定接口的唯一屬性,您必須在方法改變這樣的事情:

public static void PropertyValuesAreEquals<T> (T actual, T expected) 
{ 
    PropertyInfo[] properties = typeof (T).GetProperties(); 
    //... 
} 

現在我們可以運行這個方法:PropertyValuesAreEquals<IView> (view, view2);

不要忘記約<IView>參數;否則編譯器可能會使用FormMain類型作爲泛型類型參數。

0

你可以考慮使用泛型,在上面的邏輯中,你應該檢查屬性是否存在於提供的對象中並且可以被讀取。

public static void PropertyValuesAreEquals<T>(T actual, T expected) 
    where T : class { 
    var properties = typeof(T).GetProperties().Where(p => p.CanRead); 
    foreach (var property in properties) { 
     var expectedValue = property.GetValue(expected, null); 
     var actualValue = property.GetValue(actual, null); 

     if (actualValue is IList) 
      AssertListsAreEquals(property, (IList)actualValue, (IList)expectedValue); 
     else if (!Equals(expectedValue, actualValue)) 
      Assert.Fail("Property {0}.{1} does not match. Expected: {2} but was: {3}", property.DeclaringType.Name, property.Name, expectedValue, actualValue); 
    } 
} 

而且沒有類型的規定,你的情況作爲

Assert.IsTrue(Helper.PropertyValuesAreEquals<IView>(view, view2)); 
0

你也可以使用一個通用的功能,「實際」和「預期」。

public static void PropertyValuesAreEquals<T>(object actual, object expected) 
{ 
    Assert.IsTrue(actual is T); 
    Assert.IsTrue(expected is T); 

    foreach (var property in typeof(T).GetProperties().Where(o => o.CanRead)) 
    { 
     var actualValue = property.GetValue(actual, null); 
     var expectedValue = property.GetValue(expected, null); 

     ... 
    } 
}