【问题标题】:Why does Panel from System.Web.UI.WebControls.Panel returns null in unit testing using C#?为什么 System.Web.UI.WebControls.Panel 的 Panel 在使用 C# 的单元测试中返回 null?
【发布时间】:2021-12-27 13:06:03
【问题描述】:

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

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

我在模块中有另一个类 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。

【问题讨论】:

标签: c# sharepoint sharepoint-online sharepoint-designer sharepoint-2019


【解决方案1】:

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

这就是我所做的:

public void HidePanelControl_ShouldSetVisibleToFalse() {
    Panel panel = new Panel();
    JEUploadWorkflowLinkWPUserControl 
    userControl = new JEUploadWorkflowLinkWPUserControl();
    userControl.HidePanelControl(panel);
    Assert.IsFalse(panel.Visible);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多