asp.net unit testing - how to handle redirects

  • Thread starter Wiktor Zychla [C# MVP]
  • Start date
W

Wiktor Zychla [C# MVP]

I try to figure out how to write unit tests for asp.net. I am using the
Microsoft.VisualStudio.TestTools.UnitTesting.Web namespace with Visual
Studio 2008.

The simple case work as expected, for example:

Page page = TestContext.RequestPage;
Assert.IsTrue( page.Request.Url.ToString().EndsWith( "default.aspx" ) );

according to the documentation the PrivateObject class is a helper class for
invoking private methods like event handlers.

This works great:

Button b = (Button)page.FindControl( "Button1" );
PrivateObject po = new PrivateObject( page );
po.Invoke( "Button1_Click", b, EventArgs.Empty );

the problem occurs when the Button1_Click actually REDIRECTS to another
page.

void Button1_Click( object sender, EventArgs e )
{
// comment this call and there will be no problems with the test
Response.Redirect( "default2.aspx" );
}

in such case the tests fails with "The communication channel with ASP.NET
could not be configured." followed by some localized information from the
development server meaning that the "the service cannot be found".

has anyone observed such behaviour?

if this is so then how do we handle redirects in the test framework (this
would be one of fundamental requirements!)?

Regards,
Wiktor Zychla
 
C

Cowboy \(Gregory A. Beamer\)

You use another testing framework that tests using the browser and is able
to handle page changes.

Personally, I would consider putting all of the business code into libraries
and testing without actually dinking with the UI. Or, I would consider
moving to some form of MVC Framework, which is much more conducive to
testing.

I do not find testing UI as part of a unit test as a fundamental
requirement, but I see UI as a means for a user to interact with an
application, at least when you have true separation of layers, so I also see
testing a bit different than one who tests through the UI. There is, of
course, a time to test UI, so I am not criticizing. I just do not see the VS
method as a great means to test UI in an automated fashion.
 
S

sloan

I would second that idea...and say it like this"

"get the business logic out of the presentation layer...and Unit Test the
business logic".

...

there is one place in an app I'm working on right now...that the UI flow is
important.
However, I was able to devise a plan to still push that logic down into the
business layer, and have it return enums and such about what it is supposed
to do next.
That way....I can still unit test it outside of the presentation layer.

And then the presentation layer just responds to the return values of the
enums.

Something like this

enum WhatToDoNext
{
GoToEmployeeEditPage ,
GoToEmployeeSearchPage ,
GoToEmployeeMainPage
}

and then I have simple code in the web app to respond


switch myenum
{
case GoToEmployeeEditPage :
Response.Redirect("/Employees/EmployeeEdit.aspx");
break;
case GoToEmployeeSearchPage :
Response.Redirect("/Employees/EmployeeSearch.aspx");
break;
case GoToEmployeeMainPage:
Response.Redirect("/Employees/EmployeeMain.aspx");
break;

}


Something like that. This will also make it easier if I ever need to
develop a winforms version...since I won't have to duplicate code. All I
need to do is "pop a form in a winforms app", based on the return enum.


Anyway, there are fancier UI frameworks out there,,....but this is my po
man's version which makes maintenance and UnitTesting easier for me.



I will also recommend
Pragmatic Programmer NUnit Testing in C# (or something like that)....even
though its NUnit and not Microsoft flavor.
But the concepts are the same in either framework.
 
W

Wiktor Zychla [C# MVP]

I would second that idea...and say it like this"
"get the business logic out of the presentation layer...and Unit Test the
business logic".

thank you both for your answers. I am deeply aware of the "separate ui and
business logic" approach and I know that mvc frameworks are more convenient
when it comes to testing. I write unit tests for years using nunit and other
testing frameworks.

however, your answers worry me in a sense that the unit testing framework
shipped with visual studio is flawed since it acts like asp.net testing
framework while it is not able to handle one of the fundamental asp.net
aspects - redirecting.

as other asp.net testing frameworks show (like nunit for asp), redirects ARE
handled by the testing framework. this is expected and fundamental as I said
as web applications redirect more often than seldom.

thank you once again,
Wiktor Zychla
 
S

sloan

I use NUnit.

I had a decent talk about NUnit at TechEd2007 last summer.

Basically, we had the same thought. Just because MS packages it into VS,
doesn't make it the best.


He has a horse in the race of course... I really don't.


I was more affected by the VS2005 PriceTag.
 
A

alex

I use NUnit.

I had a decent talk about NUnit at TechEd2007 last summer.

Basically, we had the same thought.  Just because MS packages it into VS,
doesn't make it the best.

He has a horse in the race of course... I really don't.

I was more affected by the VS2005 PriceTag.

message







- Show quoted text -

You can also try SWExplorerAutomation SWEA from http://webiussoft.com.
SWEA records, replays automation scripts and generates C# or VB.NET
script code. SWEA supports all Web browser UI elements: form
controls, frames, windows and html dialogs, popup windows, tables
(transformed to DataTable) and more. SWEA was specially designed to
automate complex DHTML/AJAX applications.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top