Hardcore. That exception is thrown when the application is missing a Main() method... whcih for an ASP.Net app shoudl be taken care of for you by the framework. What's your environment?
Help those who have helped you... remember to "Mark as Answered"
[TestMethod]
public void TestMethod1( )
{
var mockHelper = CreateHtmlHelper( new ViewDataDictionary( ) );
string ddls = mockHelper.AdditionalUsersDropDownList(10);
Assert.IsNotNull(ddls);
Assert.IsTrue(ddls.Length > 0);
}
public HtmlHelper CreateHtmlHelper( ViewDataDictionary viewData )
{
var stringWriter = new StringWriter( );
var routeData = new RouteData( );
var accountController = new AccountController( );
var tempData = new TempDataDictionary( );
var mockViewDataContainer = new Mock<IViewDataContainer>( );
var httpRequest = new HttpRequest( string.Empty, "http://localhost/", string.Empty );
var httpResponse = new HttpResponse( stringWriter );
var httpContext = new HttpContext( httpRequest, httpResponse );
var httpContextWrapper = new HttpContextWrapper( httpContext );
var requestContext = new RequestContext( httpContextWrapper, routeData );
var controllerContext = new ControllerContext( requestContext, accountController );
var mockView = new Mock<IView>( );
var viewContext = new ViewContext( controllerContext, mockView.Object, viewData, tempData );
return new HtmlHelper( viewContext, mockViewDataContainer.Object );
}
If everything happens for a reason what is the reason for this error?
Hey, I think that you're culprit is probably all the 'live' context objects you're trying to use (e.g. new HttpRequest; new HttpContext, etc). In my experience, there's a lot of 'gotchas' to trying to set those up in a valid state. Rob Conery did an HttpSimulator
class that can handle it for you, but the better path in *most* cases (not all) is to not use those objects at all when you can avoid it. In this case, I think you can avoid it.
I did the following (albeit simpler) test to show you what I mean.
[TestMethod]
public void LabeledTextBoxReturnsLabelWithTextbox()
{
// Arrange
string expectedOutput = "<input id=\"Name\" name=\"Name\" type=\"text\" value=\"Joe\" />";
var context = new ViewContext();
var dataMock = new Mock<IViewDataContainer>();
ModelState state = new ModelState();
state.Value = new ValueProviderResult("Joe", "Joe", CultureInfo.CurrentCulture);
ViewDataDictionary data = new ViewDataDictionary();
data.ModelState.Add("Name",state);
dataMock.Setup(d => d.ViewData).Returns(data);
HtmlHelper helper = new HtmlHelper(context, dataMock.Object);
// Act
string html = helper.LabeledTextBox("Name");
// Assert
Assert.IsNotNull(html);
Assert.AreEqual(expectedOutput, html,true);
}
Note that while I had to use 'real' ModelState and ViewDataDictionary objects, I didn't have to have dependencies on the HttpContext or its children at all. If I did need that dependency, I'd just create a Mock<HttpContextBase>() and do setups for whatever
I needed for that particular test.
Help those who have helped you... remember to "Mark as Answered"
The exception occurs when I call an html helper from the context of a unit test.
ie.
[TestMethod]
public void Test1()
{
HtmlHelper helper = // logic to create html helper instance
string result = helper.DropDownList("testDropDownList");
}
If everything happens for a reason what is the reason for this error?
OK, can not replicate. The following code tests out fine:
[TestMethod]
public void RegularDropDownTest()
{
//Arrange
var context = new ViewContext();
var dataMock = new Mock<IViewDataContainer>();
ModelState state = new ModelState();
var list = new List<SelectListItem> { new SelectListItem { Text = "Joe", Value = "1" } };
state.Value = new ValueProviderResult(list, "1", CultureInfo.CurrentCulture);
ViewDataDictionary data = new ViewDataDictionary();
data.ModelState.Add("Name", state);
data["Name"] = list;
dataMock.Setup(d => d.ViewData).Returns(data);
HtmlHelper helper = new HtmlHelper(context, dataMock.Object);
// Act
string html = helper.DropDownList("Name");
// Assert
Assert.IsNotNull(html);
Assert.IsTrue(html.StartsWith("<select"));
}
Help those who have helped you... remember to "Mark as Answered"
mpaterson
Contributor
4619 Points
1316 Posts
System.EntryPointNotFoundException: Entry point was not found. when unit testing html helpers
Oct 10, 2009 03:33 PM|LINK
I'm trying to unit test a very simple html helper but keep getting the following exception:
System.EntryPointNotFoundException: Entry point was not found.
public static string AdditionalUsersDropDownList( this HtmlHelper helper, int additionalUsers )
{
Dictionary<int, string> items = new Dictionary<int, string>( );
items.Add( 0, "None" );
for ( int i = 1; i <= 30; i++ )
{
items.Add( i, i.ToString( ) );
}
SelectList list = new SelectList( items, "Key", "Value", additionalUsers );
return helper.DropDownList( "AdditionalUsers", list );
}
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">public static string AdditionalUsersDropDownList( this HtmlHelper helper, int additionalUsers )</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Dictionary<int, string> items = new Dictionary<int, string>( );</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> items.Add( 0, "None" );</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> for ( int i = 1; i <= 30; i++ )</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> items.Add( i, i.ToString( ) );</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> SelectList list = new SelectList( items, "Key", "Value", additionalUsers );</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return helper.DropDownList( "AdditionalUsers", list );</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div>paul.vencill
Contributor
6716 Points
1358 Posts
Re: System.EntryPointNotFoundException: Entry point was not found. when unit testing html helpers
Oct 11, 2009 02:26 AM|LINK
Hardcore. That exception is thrown when the application is missing a Main() method... whcih for an ASP.Net app shoudl be taken care of for you by the framework. What's your environment?
mpaterson
Contributor
4619 Points
1316 Posts
Re: System.EntryPointNotFoundException: Entry point was not found. when unit testing html helpers
Oct 11, 2009 02:37 AM|LINK
Haha! I'm glad to hear that this is a hardcore issue and not a pansy issue! I'm running win7 and VS2010 beta.
The issue only seems to happen when I try to unit test an html helper that attempts to use a built in html helper.
ie: return helper.DropDownList(...);
paul.vencill
Contributor
6716 Points
1358 Posts
Re: System.EntryPointNotFoundException: Entry point was not found. when unit testing html helpers
Oct 12, 2009 04:12 PM|LINK
What's your unit test look like? I setup a test using the conditions you described (though on Vista and VS 2k8) and had no issues.
mpaterson
Contributor
4619 Points
1316 Posts
Re: System.EntryPointNotFoundException: Entry point was not found. when unit testing html helpers
Oct 12, 2009 04:53 PM|LINK
[TestMethod] public void TestMethod1( ) { var mockHelper = CreateHtmlHelper( new ViewDataDictionary( ) ); string ddls = mockHelper.AdditionalUsersDropDownList(10); Assert.IsNotNull(ddls); Assert.IsTrue(ddls.Length > 0); }public HtmlHelper CreateHtmlHelper( ViewDataDictionary viewData ) { var stringWriter = new StringWriter( ); var routeData = new RouteData( ); var accountController = new AccountController( ); var tempData = new TempDataDictionary( ); var mockViewDataContainer = new Mock<IViewDataContainer>( ); var httpRequest = new HttpRequest( string.Empty, "http://localhost/", string.Empty ); var httpResponse = new HttpResponse( stringWriter ); var httpContext = new HttpContext( httpRequest, httpResponse ); var httpContextWrapper = new HttpContextWrapper( httpContext ); var requestContext = new RequestContext( httpContextWrapper, routeData ); var controllerContext = new ControllerContext( requestContext, accountController ); var mockView = new Mock<IView>( ); var viewContext = new ViewContext( controllerContext, mockView.Object, viewData, tempData ); return new HtmlHelper( viewContext, mockViewDataContainer.Object ); }
paul.vencill
Contributor
6716 Points
1358 Posts
Re: System.EntryPointNotFoundException: Entry point was not found. when unit testing html helpers
Oct 12, 2009 05:54 PM|LINK
Hey, I think that you're culprit is probably all the 'live' context objects you're trying to use (e.g. new HttpRequest; new HttpContext, etc). In my experience, there's a lot of 'gotchas' to trying to set those up in a valid state. Rob Conery did an HttpSimulator class that can handle it for you, but the better path in *most* cases (not all) is to not use those objects at all when you can avoid it. In this case, I think you can avoid it.
I did the following (albeit simpler) test to show you what I mean.
[TestMethod] public void LabeledTextBoxReturnsLabelWithTextbox() { // Arrange string expectedOutput = "<input id=\"Name\" name=\"Name\" type=\"text\" value=\"Joe\" />"; var context = new ViewContext(); var dataMock = new Mock<IViewDataContainer>(); ModelState state = new ModelState(); state.Value = new ValueProviderResult("Joe", "Joe", CultureInfo.CurrentCulture); ViewDataDictionary data = new ViewDataDictionary(); data.ModelState.Add("Name",state); dataMock.Setup(d => d.ViewData).Returns(data); HtmlHelper helper = new HtmlHelper(context, dataMock.Object); // Act string html = helper.LabeledTextBox("Name"); // Assert Assert.IsNotNull(html); Assert.AreEqual(expectedOutput, html,true); }Note that while I had to use 'real' ModelState and ViewDataDictionary objects, I didn't have to have dependencies on the HttpContext or its children at all. If I did need that dependency, I'd just create a Mock<HttpContextBase>() and do setups for whatever I needed for that particular test.
mpaterson
Contributor
4619 Points
1316 Posts
Re: System.EntryPointNotFoundException: Entry point was not found. when unit testing html helpers
Oct 12, 2009 06:03 PM|LINK
Thanks very much. I'll give this a try when I get home.
mpaterson
Contributor
4619 Points
1316 Posts
Re: System.EntryPointNotFoundException: Entry point was not found. when unit testing html helpers
Oct 12, 2009 10:11 PM|LINK
I still get the same exception.
The exception occurs when I call an html helper from the context of a unit test.
ie.
[TestMethod]
public void Test1()
{
HtmlHelper helper = // logic to create html helper instance
string result = helper.DropDownList("testDropDownList");
}
mpaterson
Contributor
4619 Points
1316 Posts
Re: System.EntryPointNotFoundException: Entry point was not found. when unit testing html helpers
Oct 12, 2009 11:48 PM|LINK
So I've done some playing around and this exception only seems to occur when trying to use Html.DropDownList.
Html.ActionLink seems to work okay....
paul.vencill
Contributor
6716 Points
1358 Posts
Re: System.EntryPointNotFoundException: Entry point was not found. when unit testing html helpers
Oct 13, 2009 02:53 AM|LINK
OK, can not replicate. The following code tests out fine:
[TestMethod] public void RegularDropDownTest() { //Arrange var context = new ViewContext(); var dataMock = new Mock<IViewDataContainer>(); ModelState state = new ModelState(); var list = new List<SelectListItem> { new SelectListItem { Text = "Joe", Value = "1" } }; state.Value = new ValueProviderResult(list, "1", CultureInfo.CurrentCulture); ViewDataDictionary data = new ViewDataDictionary(); data.ModelState.Add("Name", state); data["Name"] = list; dataMock.Setup(d => d.ViewData).Returns(data); HtmlHelper helper = new HtmlHelper(context, dataMock.Object); // Act string html = helper.DropDownList("Name"); // Assert Assert.IsNotNull(html); Assert.IsTrue(html.StartsWith("<select")); }