BT

Navigation Makes ASP.NET WebForms More Testable

by Roopesh Shenoy on Jun 18, 2012 |

Navigation is a library that makes ASP.NET Web Forms more testable by moving the CRUD logic out of the codebehind files. Graham Mendick’s article “Unit Testing in the Navigation..” explores some of the features and how unit tests can be written.

What does this library do? It allows

  • Controls to be bound to controller methods in separate classes
  • Navigational binding

all inside your aspx page mark-up. For e.g., the markup for binding a FormView to its controller methods would look something like this -

<asp:FormView ID="Question" runat="server" DataSourceID="QuestionDataSource" DefaultMode="Edit">
<EditItemTemplate>
</EditItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="QuestionDataSource" runat="server" SelectMethod="GetQuestion" UpdateMethod="UpdateQuestion" TypeName="Survey.SurveyController" DataObjectTypeName="Survey.Question" />

where the GetQuestion and UpdateQuestion are controller methods implemented in an external class named SurveyController – they cannot directly access the controls, but the data is passed to them by the Navigation Library. Survey.Question refers to a ViewModel that is used in this example for transferring the data.

Why does this simplify unit testing? Because now, the business logic is in a separate POCO class with no web-specific behaviour. And this can be tested more easily. See the article for more details. 

As we have already covered on InfoQ, WebForms 4.5 gets model binders out-of-the-box – however this library allows the controller methods to be put in a separate class instead of the code-behind file which seems better for testability and reusability. If interested, you can download the Navigation NerdDinner walkthrough

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT