<p>Alright.</p>

<p>So I figure it's about time I get into unit testing, since everyone's been banging on about it for long enough.  I've installed NUnit and gone through a few "intro to unit testing" type tutorials.</p>

<p>I'm currently putting together a small framework to help with the rebuild of one of our web apps, so I've created a VS2008 project for my framework and I want to unit test it as I go.</p>

<p>How on earth do I go about unit testing the WebControls?  The methods are all protected or private, and since it's a framework, there isn't much else but WebControls.</p>

<p>Any pointers?</p>

<p>Burns</p>

[{'answer_id': 31160, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 3, 'selected': False, 'text': '<p>Ues the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx" rel="nofollow noreferrer"><code>assembly:InternalsVisibleTo</code></a> attribute and you\'ll be able to access those private members.</p>\n\n<p>Put it in your webcontrol project\'s <em>AssemblyInfo.cs</em> (under <em>Properties</em> node)</p>\n\n<pre><code>[assembly:InternalsVisibleTo("YourTestProjectName")]\n</code></pre>\n'}, {'answer_id': 31165, 'author': 'Sean Chambers', 'author_id': 2993, 'author_profile': 'https://Stackoverflow.com/users/2993', 'pm_score': 2, 'selected': False, 'text': '<p>You have found the biggest pain point of ASP.NET. As far as sealed, private classes that hinder unit testing.</p>\n\n<p>This is the main reason that TDD people will use a MVC framework (ASP.NET MVC, Castle MonoRail) as it provides a clear seperation from your view templates and your controller logic. The controllers are fully testable.</p>\n'}, {'answer_id': 31206, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 4, 'selected': True, 'text': '<p>You can do model-view-controller or model-view-presenter type architectures without using a full blown framework. You already found out that unit-testing ui-components is difficult. There are ways around that but you probably don\'t want to go that route. Usually this will make your tests very hard to maintain, more maintenance nightmare\'s is something programmers can do without :-)</p>\n\n<p>Try to separate out the functionality you want to test in a "controller" or "presenter" class. Then test that class. To make it more testable you can hide the usercontrol class (the view) behind an interface and make the controller or presenter talk to the view through the interface. That way you can mock up the view in your tests.</p>\n\n<p>I know this sounds like a lot of work and it seems like a workaround but if you get used to this it\'s a realy nice architecture that makes it far easier to change ui behaviour. You can always start using a "real" mvc framework when you realy need it :-)</p>\n'}, {'answer_id': 31211, 'author': 'Craig', 'author_id': 2894, 'author_profile': 'https://Stackoverflow.com/users/2894', 'pm_score': 0, 'selected': False, 'text': '<p>The MVC framework mentioned above is the best way to test what the control does.  However testing how it works is a bit different.</p>\n\n<p>This is totally off the cuff but you could make the user control expose some protected methods and properties to return validation information and then have a testing user control inherit it.  That control could populate fields, press buttons and what not.  Kind of messy but it could work.</p>\n'}, {'answer_id': 42061, 'author': 'Slavo', 'author_id': 1801, 'author_profile': 'https://Stackoverflow.com/users/1801', 'pm_score': 0, 'selected': False, 'text': '<p>You can also take a look at this <a href="http://www.ayende.com/Blog/archive/2007/09/03/Rhino-Igloo-ndash-MVC-Framework-for-Web-Forms.aspx" rel="nofollow noreferrer">Rhino Igloo</a> framework. It is a compromised MVC framework for WebForms.</p>\n'}, {'answer_id': 669467, 'author': 'Scott Willeke', 'author_id': 51061, 'author_profile': 'https://Stackoverflow.com/users/51061', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://blogs.pingpoet.com/overflow/archive/2004/03/18/474.aspx" rel="nofollow noreferrer">This</a> is an old article by now, but I was using NUnitASP to write nunit tests for asp.net WebControls in 2004. That article gives a detailed example of testing a simple control using their concept of creating a corresponding "Tester" class that encapsulates the details of your control from you tests. The Tester can (should) also be in the same assembly as your control so can share some things between them (e.g. utility functions, constants, etc.). </p>\n\n<p>I used the technique (and others use variants of the technique) still today to test very sophisticated controls.</p>\n\n<p>I hope that is helpful.</p>\n'}, {'answer_id': 1467882, 'author': 'ulu', 'author_id': 112757, 'author_profile': 'https://Stackoverflow.com/users/112757', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://ivonna.biz/" rel="nofollow noreferrer">Ivonna</a>\ncan test WebControls in isolation, within the Asp.Net context\nJust call session.GetControl("Path.ascx") and verify that it has all necessary properties.</p>\n'}, {'answer_id': 1467906, 'author': 'WestDiscGolf', 'author_id': 33116, 'author_profile': 'https://Stackoverflow.com/users/33116', 'pm_score': 1, 'selected': False, 'text': '<p>You could also look at testing components through the browser as a user would see them using a testing framework such as <a href="http://www.artoftest.com/products/webaii.aspx" rel="nofollow noreferrer">WebAii</a>. I\'ve seen it work and its pretty cool. I\'ve also been told you can plug it into automated builds but I\'ve not seen that as of yet.</p>\n\n<p>Hope it helps ...</p>\n'}, {'answer_id': 49176870, 'author': 'Vin Shahrdar', 'author_id': 5908918, 'author_profile': 'https://Stackoverflow.com/users/5908918', 'pm_score': 0, 'selected': False, 'text': '<p>You test them like this:</p>\n\n<pre><code>[Test]\npublic void ConditionQueryBuilderTest_RendersProperHtml()\n{\n    var sw = new StringWriter();\n    var queryBuilder = new ConditionQueryBuilderStub\n    {\n        ID = "UnitTestbuilder",\n        QueryBuilderURL = @"\\SomeAspxPage\\SomeWebMethod",\n        ResetQueryBuilderURL = @"\\SomeAspxPage\\OnQueryBuilderReset",\n        FilterValuesCollection = new Dictionary&lt;int, string&gt; { {15, "Some Condition"}}\n    };\n    queryBuilder.RenderAllContents(new HtmlTextWriter(sw));\n\n    AppendLog(sw.ToString());\n\n    Assert.AreEqual(ExpectedHtml, sw.ToString()); // ExpectedHTML is the raw expected HTML\n}\n</code></pre>\n\n<p>Here is my stub:</p>\n\n<pre><code>internal class ConditionQueryBuilderStub : ConditionQueryBuilder // ConditionQueryBuilder is a WebControl\n{\n    internal void RenderAllContents(HtmlTextWriter writer)\n    {\n        RenderContents(writer);\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/31151', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/366/']