我用最少量的代码创建了一个新的解决方案,以代表我遇到的问题。这是我能够简化到最简单的程度。
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:ProjectsEntServ-ASP.NET runkTestsEntServObjectTestsEntServSessionTest.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:ProjectsEntServ2-ASP.NET runkTestsEntServObjectTestsEntServSessionTest.cs: line 83