簡體   English   中英

訪問控件的單元測試方法

[英]Unit testing methods which access controls

是否可以測試任何訪問winform控件的方法? 以下方法訪問表單上的一系列控件中的Enabled屬性。 不幸的是,我不知道如何對它進行單元測試/如果可能的話。

我非常欣賞一些指示。

    public void AccessToCsvFileVerificationInputs(bool access)
    {
        btnSelectCSVFilePath.Enabled = access;
        nudNumberOfColumns.Enabled = access;
        cbCurrencyPair.Enabled = access;

        foreach (Control input in tlpColumnDataTypes.Controls)
        {
            input.Enabled = access;
        }

        foreach (Control input in tlpColumnNames.Controls)
        {
            input.Enabled = access;
        }

        nudInitialRow.Enabled = access;
        nudFinalRow.Enabled = access;

        btnSelectErrorLogFilePath.Enabled = access;
    }

我發現最好的方法是將應用程序的設計轉換為某種形式的Model-View- *模式,其中Winforms Model-View-Presenter 1通常很適合。

這樣,您就擁有一個負責所有邏輯的演示者/控制器類,並且您擁有保存UI代碼的視圖。 在Winforms中,你通常會實現這一點,讓你的表單類實現一個IView接口,定義它可以做的一切。

然后,您將表單(將其聲明為接口)傳遞給演示者 - 構造函數Dependency Injection是傳遞它的典型方式。


因此,在您的示例中,您將AccessToCsvFileVerificationInputs方法移動到presenter類中,並向演示者傳遞實現正確接口的表單實例,並說明該表單可以執行的所有操作。

像這樣的東西:

public class CsvFilePresenter
{
    private ICsvFileView view_;
    public CsvFilePresenter(ICsvFileView view)
    {
        view_ = view;
    }

    public void AccessToCsvFileVerificationInputs(bool access)
    {
        // Code omitted for brevity
    }
}

現在,在那個你直接引用控件之類的UI功能的地方,你可以改為引用接口:

public void AccessToCsvFileVerificationInputs(bool access)
{
    view_.EnableSelectCSVFilePath = access;
    view_.EnableNumberOfColumns = access;
    view_.EnableCurrencyPair = access;

    // And so on...
}

我故意將控制集合排除在外 - 你如何處理這些集合取決於具體細節。

例如,您有這個ICsvFileView接口:

public interface ICsvFileView 
{
    bool EnableSelectCSVFilePath { get; set; }
    bool EnableNumberOfColumns { get; set; }
    bool EnableCurrencyPair { get; set; }        
}

此ICsvFileView的具體實現可能如下所示:

public partial class Form3 : Form, ICsvFileView
{
    public Form3()
    {
        InitializeComponent();
    }

    bool ICsvFileView.EnableSelectCSVFilePath
    {
        get
        {
            return btnSelectCsvFilePath.Enabled;
        }
        set
        {
            btnSelectCsvFilePath.Enabled = value;
        }
    }

    bool ICsvFileView.EnableNumberOfColumns
    {
        get
        {
            return nudNumberOfColumns.Enabled;
        }
        set
        {
            nudNumberOfColumns.Enabled = value;
        }
    }

    bool ICsvFileView.EnableCurrencyPair
    {
        get
        {
            return cbCurrencyPair.Enabled;
        }
        set
        {
            cbCurrencyPair.Enabled = value;
        }
    }
}

現在您已經完成了這項工作,您可以通過傳入一個模擬的接口實例並設置對該模擬的期望來測試邏輯的行為(由演示者表示)及其與View的交互。


請注意 ,如果所有這些看起來有點復雜和復雜,那是因為它! Winforms並沒有真正考慮到這種事情 - 其他框架如WPF也讓這一切變得更加容易。 如果你可以改變,我建議考慮一下。


1我鏈接到的網頁上的Martin Fowler已經退出了這種模式,將其轉換為兩種不同的模式 - 我會保留術語,因為它是眾所周知的。 他的新Passive View與我一直看到MVP的方式非常接近。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM