【问题标题】:Error 'Object reference not set to an instance of an object' when instantiated class of method实例化方法类时出现错误“对象引用未设置为对象的实例”
【发布时间】:2014-07-20 07:56:09
【问题描述】:

我在 MS Visual C# 2010 Express 中创建了两个项目。第一个项目有一个类 SugarcrmLogin,方法是 TheSugarCrmLoginTest()。

第二个项目有一个带有 Main 方法的类 Sugarcrm。这个项目引用了第一个项目的项目dll。

在 Main 方法中,我实例化了 SugarcrmLogin 类并调用了 SugarCrmLoginTest() 方法。

这里是第二个项目中Main方法的代码,这里我实例化了类并调用了方法:

public static void Main() 
{
    SugarcrmLogin Login; 
    Login = new SugarcrmLogin(); 
    Login.TheSugarcrmLoginTest();
}

两个项目都在 MS Visual C# 中成功构建,但是当我尝试使用 Nunit 运行第二个项目的项目 dll 时,我收到错误“对象引用未设置为对象的实例”并引用了 TheSugarCrmLoginTest( ) 方法在第一个项目中。

第一个项目

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    [TestFixture]
    public class SugarcrmLogin
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;

        [SetUp]
        public void SetupTest()
        {
            //driver = new FirefoxDriver();
            driver = new InternetExplorerDriver();
            baseURL = "http://127.0.0.1/";
            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }

        [Test]
        public void TheSugarcrmLoginTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/sugarcrm/index.php?module=Users&action=Login");
            driver.FindElement(By.Id("user_name")).Clear();
            driver.FindElement(By.Id("user_name")).SendKeys("admin");
            driver.FindElement(By.Id("user_password")).Clear();
            driver.FindElement(By.Id("user_password")).SendKeys("admin");
            driver.FindElement(By.Id("login_button")).Click();
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        private bool IsAlertPresent()
        {
            try
            {
                driver.SwitchTo().Alert();
                return true;
            }
            catch (NoAlertPresentException)
            {
                return false;
            }
        }

        private string CloseAlertAndGetItsText()
        {
            try
            {
                IAlert alert = driver.SwitchTo().Alert();
                string alertText = alert.Text;
                if (acceptNextAlert)
                {
                    alert.Accept();
                }
                else
                {
                    alert.Dismiss();
                }
                return alertText;
            }
            finally
            {
                acceptNextAlert = true;
            }
        }
    }
}

第二个项目:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    [TestFixture]
    public class Sugarcrm
    {
        public IWebDriver driver;
        private StringBuilder verificationErrors;
        public string baseURL;
        private bool acceptNextAlert = true;

        [SetUp]

        public void SetupTest()
        {
            //driver = new FirefoxDriver();
            driver = new InternetExplorerDriver();
            baseURL = "http://127.0.0.1/";
            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }

        [Test]
        public static void Main()
            {
                SugarcrmLogin Login;
                Login = new SugarcrmLogin();
                Login.TheSugarcrmLoginTest();
            }

        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        private bool IsAlertPresent()
        {
            try
            {
                driver.SwitchTo().Alert();
                return true;
            }
            catch (NoAlertPresentException)
            {
                return false;
            }
        }

        private string CloseAlertAndGetItsText()
        {
            try
            {
                IAlert alert = driver.SwitchTo().Alert();
                string alertText = alert.Text;
                if (acceptNextAlert)
                {
                    alert.Accept();
                }
                else
                {
                    alert.Dismiss();
                }
                return alertText;
            }
            finally
            {
                acceptNextAlert = true;
            }
        }
    }
}

【问题讨论】:

  • 可以下断点,启动后查看Login变量是否为空
  • 这听起来很可能是错误被引发inside TheSugarcrmLoginTest(),在这种情况下没有代码(更新:现在在编辑中提供)我们甚至不能希望告诉你什么;但是:堆栈跟踪是什么?
  • @YuliamChandra 虽然有一种使new SomeClass()返回null的方法,这是一个极端边缘情况(我唯一的时候'已经曾经在极端疯狂的编码示例中看到它)

标签: c# nunit


【解决方案1】:

使用显示代码的编辑:drivernull。构造函数没有分配它,你也没有调用SetupTest() 方法,确实

不过,坦率地说,您的 Sugarcrm 单元测试不应该像这样实例化并调用另一个 (SugarcrmLogin) 单元测试,IMO。这似乎是“错误地使用了单元测试框架”。

【讨论】:

  • 我现在也在调用 SetupTest() 和 TeardownTest() 方法,但我仍然收到错误消息。这里的代码: public static void Main() { SugarcrmLogin Setup;设置 = 新 SugarcrmLogin(); Setup.SetupTest(); SugarcrmLogin 登录;登录 = 新 SugarcrmLogin(); Login.TheSugarcrmLoginTest(); SugarcrmLogin 拆解;拆解 = new SugarcrmLogin(); Teardown.TeardownTest(); }
  • @user3748074 是的,但是在哪里你得到了错误?使用调试器,卢克!
  • 当我尝试使用调试器时,我收到错误“无法直接启动具有类库输出类型的项目......”。有没有办法让调试器与类库一起工作?我正在使用 MS Visual C# 2010 Express
  • 嗨 Obi Wan (Marc;-)),我想要实现的是从 Nunit 执行一系列测试。使用 Selenium IDE,我可以执行一系列测试(例如登录、创建内容、删除内容、注销),其中浏览器不会在每次测试之间打开和关闭。在 Nunit 中,每次测试后浏览器再次关闭,结果测试执行失败(例如,注销失败,因为我不再登录)。我想要一个程序来做一个设置,调用所有的测试方法并执行它们,然后进行拆解。你有什么建议吗?
  • @user3748074 不,恐怕不是我的地区
猜你喜欢
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 2012-08-31
  • 2011-11-21
  • 2012-10-01
  • 1970-01-01
相关资源
最近更新 更多