ASP.NET Unit test troubles (odd exception error)

I created a new solution with minimal amount of code representing the problem I am facing. This is the easiest thing I could handle.

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;
        }

    }
}

      

I started with a new solution and created one class in the App_Code folder with one static method, similar to one of the methods I am having a problem with. I right clicked on the class name and click "Create Unit Tests ...". He suggested to create a new test project for me, I accepted the defaults and clicked ok. It generated the following file:

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.");
        }

    }
}

      

I tried to run a test and it tries to compile and I get a build error:

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

      

I publish a website and put a link to App_code.dll in a test project and I don't get a build error anymore. Instead, I am getting the following exception error. I put breakpoints on every line of the class, and the debugger didn't stop at any line.

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

      

0


source to share


3 answers


Re-edit:

I can't seem to solve your specific problem as an InvalidCastException as it is probably one of those red herring / rabbit problems that makes you sort out and pulls your hair out a lot. I'm sure it will have to do with the build versions differ from each other when you publish them.



I don't usually do this and you can vote for bad advice, but can I recommend you to convert your asp.net site to a web application? (either that, or move the offending code into a class library). I know this is not the answer you are looking for, but it is tough love for what I can say. You will find it much easier in the long run, and I will not go into all the benefits as I am sure you will find out about them or be able to find them.

I'm sure this will be a much faster solution to your problem ... and who really wants to publish their site every time they run their unit tests?

+1


source


It doesn't seem to me that this class has anything to do with ASP.NET or that it requires all server hosts or deployments. You have the following lines:

[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.");
}

      

Try to remove unneeded components so they read like this:

[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.");
}

      

Also, you are not using the test context at all, now it might be an idea to remove them. I.E. you can remove them:



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;
    }
}

      

It might not solve your problem, but at least it removes unnecessary code and makes it easier. If that doesn't work, can you stop any line in your test after making the changes?

Alternatively, you can try initializing your string variables if the problem is in the breakpoints and not the test itself.

Finally, why are you testing with Accessor? Your test code doesn't seem to require access to any private members, so why not use juse an instance of the class itself?

0


source


The debugger does not work with ASP.Net tests.

Use System.Diagnostics.Debugger.Break as work around

0


source







All Articles