tag:blogger.com,1999:blog-4126985520350746834.post8216325367102959922..comments2024-02-23T07:51:44.771+00:00Comments on Chris O'Brien: SharePoint 2010 and unit tests (from Visual Studio 2010/MSTest.exe)Chris O'Brienhttp://www.blogger.com/profile/10022906552670607366noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-4126985520350746834.post-28983954512008423132013-04-04T08:02:56.217+01:002013-04-04T08:02:56.217+01:00Hi Chris,
Thanks for your reply!
In my current s...Hi Chris,<br /><br />Thanks for your reply!<br /><br />In my current solution, setting the HTTPContext to null isn't quite an option. I intentionally create a 'fake' HTTPContext in my test so that i can use it's inner properties to store a web object and use that to retrieve a 'fake' SPContext in my application. I create the fake context like this:<br /><br />========<br /><br />HttpRequest request = new HttpRequest("", web.Url, "");<br />request.Browser = new HttpBrowserCapabilities();<br />HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));<br /><br />// SPContext is based on SPControl.GetContextWeb(), which looks here<br />if (HttpContext.Current.Items["HttpHandlerSPWeb"] == null)<br /> HttpContext.Current.Items["HttpHandlerSPWeb"] = web;<br /><br />return SPContext.Current;<br /><br />========<br /><br />Setting the HTTPContext to NULL results in a nullreference in my application, which gives me a big red dot in my test explorer :-)<br /><br />Do you have another suggestion to get around the 'Updates are currently disallowed on GET requests.' error, or how to create a 'fake' spcontext without the need for an HTTPContext?<br /><br />Thanks!Maartennoreply@blogger.comtag:blogger.com,1999:blog-4126985520350746834.post-42064124342698049352013-03-18T20:50:36.518+00:002013-03-18T20:50:36.518+00:00@Maarten,
I agree, amending the code being tested...@Maarten,<br /><br />I agree, amending the code being tested isn't ideal. Actually I'm surprised you get this error in a unit test - is HttpContext set to null? If not, try doing that and seeing if you still get the error.<br /><br />If I remember rightly, the security check does look for something in HttpContext.<br /><br />Cheers,<br /><br />Chris.<br /><br />Chris O'Brienhttps://www.blogger.com/profile/10022906552670607366noreply@blogger.comtag:blogger.com,1999:blog-4126985520350746834.post-67152144394039300972013-03-18T15:44:09.835+00:002013-03-18T15:44:09.835+00:00Hi Chris,
Thanks for this article. I'm trying...Hi Chris,<br /><br />Thanks for this article. I'm trying to implement unit tests in a VS2012 environment. It seems that targeting the correct framework isn't an issue in this version of VS.<br /><br />Creating a unit test which gets a reference to an SPSite works (I'm using a 'fake' SPContext and use the new SPSite(url, site.UserToken) constructor). However, when I try to update a SPListItem in the unit test, I receive the well known <i>'Updates are currently disallowed on GET requests.'</i> error.<br /><br />A solution would be to modify the original code by adding the 'web.allowunsafeupdates = true' line, however, it doesn't feel right to change the original code, for unit tests.<br /><br />Do you have an idea how to solve this problem?<br /><br />Thanks!Maartenhttp://www.dotnetquest.comnoreply@blogger.comtag:blogger.com,1999:blog-4126985520350746834.post-44968652905965101582013-01-23T15:24:34.285+00:002013-01-23T15:24:34.285+00:00Thanks for the informative post. i have two "...Thanks for the informative post. i have two "devenv.exe.config" files , one with the "devenv.exe.config with GUID". i done the "Possible additional steps" in the second one and its working.<br /><br />Thanks a lot.testhttps://www.blogger.com/profile/02039621300192731346noreply@blogger.comtag:blogger.com,1999:blog-4126985520350746834.post-51873524928747717892012-11-29T11:29:24.608+00:002012-11-29T11:29:24.608+00:00Chris
Good post.. I am having getting more rigoro...Chris<br /><br />Good post.. I am having getting more rigorous with unit testing as my Clients are increasingly asking me to code around many sp2010 short comings ;-)/. Do you personally (type) mock stuff before going down the route you describe in the post?<br />BTW see you on the 18th..westerdaledhttps://www.blogger.com/profile/04978235432543060653noreply@blogger.comtag:blogger.com,1999:blog-4126985520350746834.post-36676152040059833542012-09-05T08:02:12.747+01:002012-09-05T08:02:12.747+01:00Thanks for the detailed information. I was going m...Thanks for the detailed information. I was going mad, because applying SP1 did not make any difference. I would say almost all developer will have to manually edit the desenv.exe.config, who doesn't have at least one pluggin or addon?<br /><br />One detail: the link you provided (Additional steps...) points to the VS 2012 article, where that section does not exist. The correct link is thus http://msdn.microsoft.com/en-us/library/gg601487(v=vs.100).aspx<br /><br />Halfhttps://www.blogger.com/profile/18156920435495331175noreply@blogger.com