【问题标题】:Unit testing "object reference not set to an instance " at NUnit在 NUnit 进行单元测试“对象引用未设置为实例”
【发布时间】:2015-03-05 10:06:19
【问题描述】:

我有一个 ASP.Net 项目和 Nunitasp 框架用于单元测试,当我尝试测试对象(NugetplatformModel)值时,我在 account.aspx.cs 文件中有一个对象,我得到“对象引用未设置为实例“ 错误, 我的帐户页面代码如下所示

 public partial class Account : System.Web.UI.Page
{
    public NugetPlatformModel NugetPlatformModels;
    public string result = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!WebSecurity.IsAuthenticated)
        {
            Response.Redirect("/login", true);
        }
        else
        {
            result = "success";
            NugetPlatformModels = new NugetPlatformModel();
        }

}

我的测试用例代码如下

        [Test]
    public void AccountPage_ValidCredential_AccessModel()
    {
        Browser.GetPage(domain + "account");
        string ExpectedPage = domain + "account";
        logon();
        Account acccountPage = new Account();
        AssertEquals("success", acccountPage.result);
        AssertEquals("should have license",true,acccountPage.NugetPlatformModels.IsHavingLicense);

    }

如何访问和测试变量背后的代码?开始测试时,已经分配了 NUgetplatformmodel 我已经通过调试检查了它,但是在 nunit gui 中它显示空引用错误之后,我认为在测试用例中访问变量时存在问题..请帮助我..

【问题讨论】:

  • where(即哪一行)抛出了 NullReferenceException?如果没有这些信息,就很难为您提供帮助。
  • AssertEquals("success", acccountPage.result); AssertEquals("应该有许可证",true,accountPage.NugetPlatformModels.IsHavingLicense);这两行在 nunit 测试中出现异常,感谢您的回复..

标签: nunit unit-testing


【解决方案1】:

您的代码似乎不完整。从我在这里看到的情况来看,您的帐户需要运行 Page_Load 才能填充结果和 NugetPlatformModels。但是我看不到这个方法是如何在你的测试中启动的。它是从 Account 的构造函数运行的吗?

如果您将帐户的所有代码都放在帖子中会很有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2016-06-12
    • 2020-11-03
    • 1970-01-01
    • 2012-07-23
    相关资源
    最近更新 更多