0

我有一个名称的设计器类,ExpenseReceipts.aspx.designer.cs其中部分类 ExpenseReciepts 包含

public partial class ExpenseReceipts {
    protected global::System.Web.UI.WebControls.Panel ResultPanel;
}

我在使用 this 的模块中有另一个类 ExpenseReceipts.aspx.cs ResultPanel

public partial class ExpenseReceipts : LayoutsPageBase
{
public void HandleRecordsNotFound()
        {
            ResultPanel.Visible = false;
            NoItemsMessage.Text = "No Items Found!!!";
            NoItemsMessage.ForeColor = System.Drawing.Color.Red;
            NoItemsPanel.Visible = true;
        }
}

就生产代码而言,这可以正常工作,但是当我尝试为其编写单元测试时会出现问题。我有以下方法试图在这里填充所有内容。

        [TestMethod]
        public void HandleRecordsNotFound_ShouldMakeNoResultPanelVisible()
        {
            using (ShimsContext.Create())
            {
                bool flag = false;

                ShimControl.AllInstances.VisibleSetBoolean = (PANEL, BOOLEAN) => { };
                ShimLabel.AllInstances.TextSetString = (LABEL, TEXT) => { };
                ShimWebControl.AllInstances.ForeColorSetColor = (WEBCONTROL, FORECOLOR) => { };
                ShimControl.AllInstances.VisibleSetBoolean = (CONTROL, BOOLEAN) => { flag = true; };
                ExpenseReceipts expenseReceipts = new ExpenseReceipts();
                expenseReceipts.HandleRecordsNotFound();
                Assert.IsTrue(flag);
            }
        }

它失败是因为 HandleRecordsNotFound 中抛出的异常说NUll pointer exception指示 - ResultPanel为空。

请帮我解决这个问题。提前致谢。PS:我正在使用 Sharepoint 2019。

4

1 回答 1

0

我遵循的 Shimming 方法是错误的。这不是为 UI 控件编写测试的正确方法。

这是我所做的:

public void HidePanelControl_ShouldSetVisibleToFalse() {
    Panel panel = new Panel();
    JEUploadWorkflowLinkWPUserControl 
    userControl = new JEUploadWorkflowLinkWPUserControl();
    userControl.HidePanelControl(panel);
    Assert.IsFalse(panel.Visible);
}
于 2021-12-02T05:38:52.720 回答