2012-10-25 159 views
0

如果您需要從另一個項目中測試非公共屬性,Microsoft單元測試嚮導會創建Accessor對象。在我的單元測試中,我創建了輔助函數,這樣我就不會在每個單元測試方法中重複相同的代碼。目前我有兩個幾乎完全相同的測試,除了一個採用標準公共對象,另一個採用Accessor版本。由於Accessor基於標準版本,因此我應該能夠擁有一個功能。使用動態對象從方法返回不同類型

我假設我可以使用泛型來完成一個簡單的演員。但在posting the question之後,我發現還有很多工作要做,包括必須更新底層對象。當我問是否存在其他方法時,這一點得到了證實。有人建議Dynamic objects might work。我不熟悉動態對象,有人可以發佈一個示例,允許我有一個共享函數並在運行時確定對象嗎?

下面是現有的兩個功能:

// Common function to create a new test record with standard Account object 
internal static Account CreateAccount(bool saveToDatabase) 
{ 
    DateTime created = DateTime.Now; 
    string createdBy = _testUserName; 

    Account account = new Account(created, createdBy); 

    account.Notes = Utilities.RandomString(1000); 

    if (saveToDatabase) 
     account.Create(); 
} 

// Common function to create a new test record with Account_Accessor 
internal static Account_Accessor CreateAccount(bool saveToDatabase) 
{ 
    DateTime created = DateTime.Now; 
    string createdBy = _testUserName; 

    Account_Accessor account = new Account_Accessor(created, createdBy); 

    account.Notes = Utilities.RandomString(1000); 

    if (saveToDatabase) 
     account.Create(); 
} 

我有這些單元測試的兩打和真實對象有10個屬性的平均值,我在這裏簡單的例子。

下面是單元測試API創建的訪問器代碼(再次,我已經降低下來,以簡化的例子):

using Microsoft.VisualStudio.TestTools.UnitTesting; 
using System; 
using System.Collections.ObjectModel; 
using System.Data; 

namespace NameHere.Bll 
{ 
    [Shadowing("NameHere.Bll.Account")] 
    public class Account_Accessor : ProjectBase_Accessor<Account> 
    { 
     protected static PrivateType m_privateType; 

     public Account_Accessor(PrivateObject value); 
     [Shadowing("[email protected]")] 
     public Account_Accessor(DateTime created, string createdBy); 

     [Shadowing("_notes")] 
     public string _notes { get; set; } 

     public static Account_Accessor AttachShadow(object value); 

     [Shadowing("[email protected]")] 
     public override void Create(); 
    } 
} 

using Microsoft.VisualStudio.TestTools.UnitTesting; 
using System; 
using System.ComponentModel; 
using System.Linq.Expressions; 

namespace NameHere.Bll 
{ 
    [Shadowing("NameHere.Bll.ProjectBase`1")] 
    public class ProjectBase_Accessor<T> : BaseShadow, INotifyPropertyChanged 
    { 
     protected static PrivateType m_privateType; 

     public ProjectBase_Accessor(PrivateObject value); 

     [Shadowing("Created")] 
     public DateTime Created { get; set; } 
     public static PrivateType ShadowedType { get; } 

     [Shadowing("[email protected]")] 
     public void add_PropertyChanged(PropertyChangedEventHandler value); 
     public static ProjectBase_Accessor<T> AttachShadow(object value); 

     [Shadowing("[email protected]")] 
     public virtual void Create(); 
    } 
} 

回答

0

這裏我在使用動態對象共享功能的第一次嘗試。它返回取決於布爾useAccessor PARAM要麼賬戶或Account_Accessor:

internal static dynamic CreateAccount(bool saveToDatabase, bool useAccessor) 
{ 
    DateTime created = DateTime.Now; 
    string createdBy = _testUserName; 
    dynamic account; 

    if (useAccessor) 
     account = new Account_Accessor(created, createdBy); 
    else 
     account = new Account(created, createdBy);  

    account.Notes = Utilities.RandomString(1000); 

    if (saveToDatabase) 
     account.Create(); 
} 

任何建議或意見,這一做法?

相關問題