ASP.NET单元测试问题(奇怪的异常错误)

问题描述:

我用最少量的代码创建了一个新解决方案,代表我遇到的问题。这是最简单的我可以得到它。ASP.NET单元测试问题(奇怪的异常错误)

namespace EntServ.BusinessObjects 
{ 
    /// <summary> 
    /// Summary description for EntServSession 
    /// </summary> 
    public class EntServSession 
    { 
     public EntServSession() 
     { 

     } 

     public static EntServSession Login(string username, string password) 
     { 
      EntServSession ret = null; 

      if (username == "test" && password == "pass") 
       ret = new EntServSession(); 

      return ret; 
     } 

    } 
} 

我开始了新的解决方案,并在App_Code文件夹中创建一个类相似,我是有问题的方法之一一个静态方法。我右键单击类名,然后单击“创建单元测试...”。它提供了为我创建一个新的测试项目,我接受了默认值,并点击好。它生成以下文件:

using EntServ.BusinessObjects; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using Microsoft.VisualStudio.TestTools.UnitTesting.Web; 
using System.Data; 

namespace EntServObjectTests 
{ 


    /// <summary> 
    ///This is a test class for EntServSessionTest and is intended 
    ///to contain all EntServSessionTest Unit Tests 
    ///</summary> 
    [TestClass()] 
    public class EntServSessionTest 
    { 


     private TestContext testContextInstance; 

     /// <summary> 
     ///Gets or sets the test context which provides 
     ///information about and functionality for the current test run. 
     ///</summary> 
     public TestContext TestContext 
     { 
      get 
      { 
       return testContextInstance; 
      } 
      set 
      { 
       testContextInstance = value; 
      } 
     } 

     #region Additional test attributes 
     // 
     //You can use the following additional attributes as you write your tests: 
     // 
     //Use ClassInitialize to run code before running the first test in the class 
     //[ClassInitialize()] 
     //public static void MyClassInitialize(TestContext testContext) 
     //{ 
     //} 
     // 
     //Use ClassCleanup to run code after all tests in a class have run 
     //[ClassCleanup()] 
     //public static void MyClassCleanup() 
     //{ 
     //} 
     // 
     //Use TestInitialize to run code before running each test 
     //[TestInitialize()] 
     //public void MyTestInitialize() 
     //{ 
     //} 
     // 
     //Use TestCleanup to run code after each test has run 
     //[TestCleanup()] 
     //public void MyTestCleanup() 
     //{ 
     //} 
     // 
     #endregion 


     /// <summary> 
     ///A test for Login 
     ///</summary> 
     // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example, 
     // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server, 
     // whether you are testing a page, web service, or a WCF service. 
     [TestMethod()] 
     [HostType("ASP.NET")] 
     [AspNetDevelopmentServerHost("%PathToWebRoot%\\EntServ2-ASP.NET\\trunk\\WWW", "/WWW")] 
     [UrlToTest("http://localhost/WWW")] 
     public void LoginTest() 
     { 
      string username = string.Empty; // TODO: Initialize to an appropriate value 
      string password = string.Empty; // TODO: Initialize to an appropriate value 
      EntServSession expected = null; 
      EntServSession actual = EntServSession_Accessor.Login(username, password); 
      Assert.AreEqual(expected, actual); 
      Assert.Inconclusive("Verify the correctness of this test method."); 
     } 

    } 
} 

我试图运行测试,它试图编译和我得到的编译错误:

Error 1 
The type or namespace name 'EntServSession' could not be found 
(are you missing a using directive or an assembly reference?) C:\Projects\EntServ-ASP.NET\trunk\Tests\EntServObjectTests\EntServSessionTest.cs 
82 
13 
EntServObjectTests 

我发布网站,并投入到App_Code参考。在测试项目中的dll,我不再收到构建错误。我反而得到以下异常错误。我在课程的每一行都放置了断点,并且调试器不会在任何行上停止。

Error Message 
Test method EntServObjectTests.EntServSessionTest.LoginTest threw exception: System.InvalidCastException: Unable to cast object of type 'EntServ2.BusinessObjects.EntServSession' to type 'EntServ2.BusinessObjects.EntServSession'.. 

Stack Trace 
EntServ2.BusinessObjects.EntServSession_Accessor.Login(String username, String password) 
EntServObjectTests.EntServSessionTest.LoginTest() in C:\Projects\EntServ2-ASP.NET\trunk\Tests\EntServObjectTests\EntServSessionTest.cs: line 83 

重新编辑:

我不能真正解决您的具体问题作为InvalidCastException的这大概是那些红鲱鱼/兔洞的问题,带你几天找出和多发一个拔出。我相信它会在发布它们时与汇编版本有所不同。

我通常不这样做,你可以投我我的坏建议,但我可以建议你转换你的asp.net网站到一个web应用程序? (或者将有问题的代码移动到类库中)。我知道这不是你正在寻找的答案,但是我很难说我能说什么。你会发现,从长远来看,这将变得容易得多,我不会深入了解所有的好处,因为我确信你会听说他们或者可以谷歌他们。

我相信,这将是一个更快的解决方案,您的问题......毕竟谁真的想每次他们运行单元测试发布他们的网站?

+0

不能使用调试器,它曾经创下了断点之前抛出异常。测试项目在同一解决方案中的单独项目下设置在不同的文件夹中,并且测试代码未与站点一起部署。 web应用程序没有assemblyinfo.cs。 – stephenbayer 2009-01-05 20:08:37

+0

好吧,我的错误,我不清楚,你使用的是一个asp.net网站项目,而不是一个web应用程序,我也误读EntLoject企业库(漫长的一天:)。所以它甚至在达到测试的第一行之前抛出异常? – Xian 2009-01-05 21:16:58

在我看来,这个类与ASP.NET没有任何关系,或者它根本不需要任何服务器主机或部署模拟。你有这些行:

[TestMethod()] 
[HostType("ASP.NET")] 
[AspNetDevelopmentServerHost("%PathToWebRoot%\\EntServ2-ASP.NET\\trunk\\WWW", "/WWW")] 
[UrlToTest("http://localhost/WWW")] 
public void LoginTest() 
{ 
    string username = string.Empty; // TODO: Initialize to an appropriate value 
    string password = string.Empty; // TODO: Initialize to an appropriate value 
    EntServSession expected = null; 
    EntServSession actual = EntServSession_Accessor.Login(username, password); 
    Assert.AreEqual(expected, actual); 
    Assert.Inconclusive("Verify the correctness of this test method."); 
} 

尝试删除不需要的成分,所以它读取这样的:

[TestMethod()] 
public void LoginTest() 
{ 
    string username = string.Empty; // TODO: Initialize to an appropriate value 
    string password = string.Empty; // TODO: Initialize to an appropriate value 
    EntServSession expected = null; 
    EntServSession actual = EntServSession_Accessor.Login(username, password); 
    Assert.AreEqual(expected, actual); 
    Assert.Inconclusive("Verify the correctness of this test method."); 
} 

而且,你不使用测试方面的话,那可能是一个想法,删除他们现在。 I.E.你可以删除这些:

private TestContext testContextInstance; 

/// <summary> 
///Gets or sets the test context which provides 
///information about and functionality for the current test run. 
///</summary> 
public TestContext TestContext 
{ 
    get 
    { 
     return testContextInstance; 
    } 
    set 
    { 
     testContextInstance = value; 
    } 
} 

这可能不是解决您的问题,但至少它消除了不必要的代码,并可以简化它。如果这不起作用,您可以在进行更改后断开测试的任何一行吗?

此外,您可以尝试初始化您的字符串变量,以防万一问题出现在断点而不是测试本身。

最后,你为什么要用Accessor进行测试?你的测试代码似乎不需要访问任何私有成员,那为什么不使用这个类本身的实例呢?

调试器不能使用ASP.Net测试。

使用System.Diagnostics.Debugger.Break作为一个替代