asp.net unit testing - how to handle redirects
asp.net unit testing - how to handle redirects
am 02.01.2008 17:51:15 von wzychla
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
Re: asp.net unit testing - how to handle redirects
am 03.01.2008 06:18:56 von NoSpamMgbworld
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.
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box!
|
*************************************************
"Wiktor Zychla [C# MVP]" wrote in
message news:%23dFBz%23VTIHA.1212@TK2MSFTNGP05.phx.gbl...
>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
Re: asp.net unit testing - how to handle redirects
am 03.01.2008 08:01:55 von 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.
"Cowboy (Gregory A. Beamer)" wrote in
message news:ubISlgcTIHA.4656@TK2MSFTNGP03.phx.gbl...
> 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.
>
> --
> Gregory A. Beamer
> MVP, MCP: +I, SE, SD, DBA
>
> *************************************************
> | Think outside the box! |
> *************************************************
> "Wiktor Zychla [C# MVP]" wrote in
> message news:%23dFBz%23VTIHA.1212@TK2MSFTNGP05.phx.gbl...
>>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
>
>
Re: asp.net unit testing - how to handle redirects
am 03.01.2008 09:47:27 von wzychla
> 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
Re: asp.net unit testing - how to handle redirects
am 04.01.2008 19:27:41 von 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.
"Wiktor Zychla [C# MVP]" wrote in
message news:%23cwdFVeTIHA.1188@TK2MSFTNGP04.phx.gbl...
>> 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
Re: asp.net unit testing - how to handle redirects
am 15.01.2008 23:38:00 von alex_f_il
On Jan 4, 1:27=A0pm, "sloan" wrote:
> I use NUnit.
>
> I had a decent talk about NUnit at TechEd2007 last summer.
>
> Basically, we had the same thought. =A0Just 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.
>
> "Wiktor Zychla [C# MVP]" wrote in
> messagenews:%23cwdFVeTIHA.1188@TK2MSFTNGP04.phx.gbl...
>
>
>
> >> I would second that idea...and say it like this"
>
> >> "get the business logic out of the presentation layer...and Unit Test t=
he
> >> business logic".
>
> > thank you both for your answers. I am deeply aware of the "separate ui a=
nd
> > 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 framewor=
k
> > 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 a=
s
> > I said aswebapplications redirect more often than seldom.
>
> > thank you once again,
> > Wiktor Zychla- Hide quoted text -
>
> - 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.