因此,我在我的應用程序中有一層將object
的一種類型映射到另一個類型中。認爲ViewModel
模型類型的映射。 ViewModel
可能具有名稱不同或模型中不存在的屬性。反之亦然。斷言2個對象相同
我想測試我的映射層,比較分配,但也允許我提供一些不同的屬性的邊緣案例處理。理想情況下,如果未檢查ViewModel
中的所有屬性,則測試將失敗。
有誰知道這樣的野獸是否已經存在?
public class CustomerViewModel
{
// This is the same as CustomerModel.CustomerName, but the names differ
public string Name { get; set; }
public int ID { get; set; }
}
public class CustomerModel
{
public string CustomerName { get; set; }
public int ID { get; set; }
}
// Would auto test the properties that match automatically. Additionaltest test for non matching. Fails if all properties aren't tested
Assert.CompareObjects(customerViewModelInstance, customerModelInstance)
.AdditionalTest("Name", "CustomerName")
.AdditionalComplexText((viewModel, model) =>
{
// do some sort of a compare of complex objects. Maybe the viewmodel has address fields, address1, address2 while the Model has an Address object with those fields.
});
這背後的驅動力是必須在代碼中手動斷言每一個屬性一個非常大的應用程序的艱鉅任務。
「這樣的野獸」會如何知道你正在比較哪些屬性與哪些屬性相比?除此之外,我認爲你能夠以自動化的方式做的最多的事情就是比較每個班級之間的*數量*屬性,以確保他們是平等的;但這並不能證明所有的任務都存在。 –
@Kyralessa。請看我的答案。有一定的方法來比較實際的屬性。 –
'IEquatable' –