kurabdurbos
kurabdurbos

Reputation: 277

Microsoft Unit Testing for ASP.NET with VS2012

I am ripping apart and putting back together a large website and want to take this opportunity to do some test driven development as the site is recreated. The issue that I am running into is how. A lot of the items that I need to test deal with session variables (or other variables) that are being set when a user logs in. But if I am testing an individual page I don't ever log in.

For example:

[TestMethod()]
[HostType("ASP.NET")]
[UrlToTest("http://localhost:64769/UsersDetail.aspx")]
public void GetCompanyId_Test()
{
    var testID = GetCompanyID();

    Assert.AreEqual("123456789", testID);

}

Now, the problem is that in order for GetCompanyID to work, it has to have variables available that are set at login.

Is this possible? Do I have to mock up the data in some way ?

Thanks

Upvotes: 2

Views: 145

Answers (1)

Aether McLoud
Aether McLoud

Reputation: 732

You'll probably have to change the GetCompanyID function to use some variables that are filled by the session in the livesystem, and by your testsetup in the unit test system. Alas, you can't mock HttpSessionState in ASP.NET - see here: How do I mock/fake the session object in ASP.Net Web forms?

Another idea would be to actually do the loginaction in the testsetup.

Upvotes: 1

Related Questions