Looking For Complete Mocking Framework For HttpContext?
Aug 25, 2010
I'm looking for as comprehensive as possible of a mock replacement and wrapper for the ASP.NET HttpContext in my applications. A comprehensive mock replacement could potentially increase the testability of my ASP.NET web applications substantially, without necessitating migrating every application to more-testable frameworks such as MVC.
Some of the features I am most interested in seeing in an HttpContext wrapper and mock framework include:
Serialized session storage (e.g., .Session).
Serialized application-scoped storage (e.g., .Application).
Per-request item storage (e.g., .Items).
HttpRequest data, such as referrers, request Uri, server variables, post data, etc.
HttpResponse data, such as status codes and content.
Local file resolution (e.g. Server.MapPath)
VirtualPathUtility for application-relative URL path resolution, which has a dependency on the ASP.NET runtime.The identity and principal (e.g., .User) for validating authentication/authorization rules.The AllErrors collection for testing error resolution in HttpModules and Global.asax.I considered writing my own interface, wrapper, and mock; however, I believe such must already exist. The variety of mock frameworks is a little overwhelming for a first-timer to absorb.
What is the most comprehensive HttpContext wrapper and mock?
View 2 Replies
Similar Messages:
Sep 7, 2010
Since I'm having problem with unit testing RenderPartialViewToString() with Moq framework [URL] I'm thinking about getting my controller directly, without using Moq for these particular test, however, how do I mocks (or set) the HttpContext for my test without using any Moq framework?
I need to able to do something similar to this, without Moq of course:
var mockHttpContext = new Mock<ControllerContext>();
mockHttpContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("n1\test");
mockHttpContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);
View 2 Replies
Apr 9, 2010
We're migrating an application to use IIS7 integrated mode. In library code that is designed to work either within the context of an HTTP request or not, we commonly have code like this:
if (HttpContext.Current != null &&
HttpContext.Current.Request != null) {
// do something with HttpContext.Current.Request
} else {
// do equivalent thing without HttpContext..
}
But in IIS7 integrated mode the check for HttpContext.Current.Request throws an exception whenever this code is called from Application_Start.
protected void Application_Start(object sender, EventArgs e)
{
SomeLibrary.DoSomethingWithHttpContextCurrentDetection();
}
Results in:
System.Web.HttpException: Request is not available in this context
How can I detect whether the request is really available without wrapping these calls in an exception handler and taking action based on whether an exception is generated or not.
Looking at HttpContext in Reflector I see it has an internal bool HideRequestResponse field but it's internal so I can only get to it with reflection and that's fragile. Is there a more official/approved way to determine if it's ok to call HttpContext.Request?
This blog post about the subject says not to use HttpContext, but how, in generic library code, can you determine if it's ok to use HttpContext?
http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx
I'm using the work-around mentioned there which is to use Application_BeginRequest and an initialized field to only initialize once as part of BeginRequest, but that has to be done in every calling application whereas I'd prefer to make the library code more robust and handle this situation regardless of where it's called from.
View 4 Replies
Jun 21, 2010
I'm trying to unit test the logOn method of my .Net 4.0 MVC application. I'm pretty sure I need to mock a bunch of objects because I don't want to hit the actual database and I only want to simulate an HTTPcontext in a limited and specific way (if I have to do it at all). All the examples I've found online use mocking frameworks like Moq, RinoMocks or nMock. MVC is all about Test Driven Development, there must be some way to do simple tests without using third party software.
The specific problem,The basic problem is that the controller I'm testing calls Response.Cookies.Add(...) but since the unit test is not in an HTTPContext, Response is null and I get an exception.
My research so far,I understand there is a layer of abstraction which will help me somehow. For example HttpResponse inherits from HttpResponseBase. I think I'm supposed to make a class that inherits from one of the MVC base classes, overload some of the members and then
call the method I want to test in a way that gives it my object instead of one it would normally use.
The question is how to do that. One would need to set up existing code with the ability to construct future versions of objects. Intuitively however, it would seem that existing code could only be and use functionality that existed at the time it was written.
Fortunately, a dynamic runtime means that the code which is actually run can be determined things that change after it is written like config files and HTTP headers.
One way to do that is for the existing code to use some method typically referred to as a factory that takes a string and returns an instance of an object.I conclude that I should look through the code for this factory and figure out how to manipulate it.
My background,I've recently graduated with a degree in Computer Science. Now at my new job, I've been assigned to write unit tests for a large project. Admittedly, I'm new to MVC, many aspects of C# and large projects in general. I may have made simple mistakes or incorrect assumptions in my analysis of this problem. I will do my best to stay active on this post and make it useful for others in situations similar to mine.
View 2 Replies
Feb 26, 2010
I am looking for some guidance
\
-Rhino.Mocks
-Moq
-NMock
-TypeMock (not free)
View 1 Replies
Jun 1, 2010
I have a cookie which my controller uses.
How should i mock the cookie?
View 3 Replies
Apr 6, 2010
I am new to testing.I have to test some C# classes.Kindly let me know what is mocking and why some mocking framework like Rhino mock is preferred over nUint?
View 1 Replies
Jun 12, 2010
I need to create Unit Tests for an ASP.NET MVC 2.0 web site. The site uses Windows Authentication.I've been reading up on the necessity to mock the HTTP context for code that deals with the HttpContext. I feel like I'm starting to get a handle on the DI pattern as well. (Give the class an attribute of type IRepository and then pass in a Repository object when you instantiate the controller.)
What I don't understand, however, is the proper way to Mock the Windows Principal object available through User.Identity. Is this part of the HttpContext? have a link to an article that demonstrates this (or a recommendation for a book)?
View 2 Replies
Nov 23, 2010
I have been experimenting with code that will clear all of the cookies in an HttpContext.Response.Initially, I used this:
DateTime cookieExpires = DateTime.Now.AddDays(-1);
for (int i = 0; i < HttpContext.Request.Cookies.Count; i++)
{
HttpContext.Response.Cookies.Add(
new HttpCookie(HttpContext.Request.Cookies[i].Name, null) { Expires = cookieExpires });
}
this will error with an OutOfMemoryException because the for loop never exits - each time you add a cookie to the Response, it also gets added to the `Request.
View 1 Replies
Nov 23, 2010
i'm using an example in which i can fake the session.It's for use in a unittest of a mvc controller.In the test i create a controller and then i do this:
FakeHttpContext httpctx = new FakeHttpContext(null,null,null,null,mSessionItems );
ControllerContext ctx2 = new ControllerContext(httpctx,new RouteData(), target);
here mSessionItems is my session and target is my instance of a controller,and indeed, when i'm in a controller reading this.ControllerContext.HttpContext.Session, i have a session, great!but.... i also read the session outside the controller, and there i use HttpContext.Current.Session, and that is null (or actualy, the HttpContext.Current is null).
View 1 Replies
May 25, 2010
I'll be starting a complete re-write of a system in the coming weeks. I'll be using ASP.NET MVC 2.I'm still trying to determine what I should be using as far as TDD, mocking, and IOC is concerned. I was thinking:
VSTEST for writing my TDD tests (or possibly nUnit) Moq as my mocking framework (for creating doubles and fakes) Moq for IOC development Not sure exactly what the ideal setup should be and I'm continuing to research what's available. I'd like some feedback from the community on the most optimal toolsets for TDD, mocking, and IOC.
View 10 Replies
Mar 28, 2011
I've see n a lot of discussions surrounding HttpSessionState and asp.net MVC.I'm trying to write tests for an asp.net application and to mock the HttpSessionState and if so, how?I'm currently using Rhino Mocks and Nunit
View 1 Replies
May 24, 2010
Microsoft Ajax can expose webservices which respond with json or xml depending on configuration. I would like to mock these services using soap ui. Using the wsdl I can do this to mock the services in the case where xml is returned, however how can I mock the response when JSON is returned?
View 1 Replies
May 17, 2010
I have a class which I store in session. Lets say Class A, having an attribute theList. I have a member function that adds an item to theList and returns the count of my list.So I mock my class and set theList to "1,2,3" for my unit test, and test my add function by adding "4".
It returns count "3" instead of "4".
View 3 Replies
Jul 7, 2010
here Im trying to write a test can I mock a HttpRequestBase to return post values like this? please help as its quite urgent, how can I acheive this?
var collection = new NameValueCollection();
collection.Add("Id", "1");
collection.Add("UserName", "");
[code]...
View 1 Replies
Feb 9, 2010
So I've got this method called LoginUser:
public void LoginUser(out SystemUser userToLogin, string username)
Having just started with Rhino Mocks, I'm having a little trouble mocking a call and return value from this method while testing my Presenter code. What's the correct syntax in this instance?
View 1 Replies
Jul 15, 2010
I'm writing unit tests in ASP.NET 4.0 web application (ASP.NET Forms, not MVC). There are several spots in the code where I call the ServerVariables collection to call variables like REMOTE_ADDR. Since my unit tests do not actually initiate HttpRequests when executing my code, things like ServerVariables are Null and therefore error when I try to call HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
All the solutions I've found to address this issue refer to MVC and so they assume that HttpRequest derives from HttpRequestBase, which it does in MVC but not in ASP.NET Forms.I tried using Moq but you can't mock a sealed class, and HttpRequest is unfortunately sealed with no interface.
View 1 Replies
May 18, 2010
I host a website that has recently been upgraded from .NET Framework 2.0 to .NET Framework 4.0. Within the website, I have an instance of BlogEngine which still requires .NET Framework 2.0. Each website is in it's own application pool. My issue is that when I browse to the blog portion of my website, I receive a 500.19 error with the following information...
The configuration section 'system.web.extensions' cannot be read because it is missing a section declaration The web.conf file that is listed in the error message is the config file that belongs to my root website which is running .NET Framework 4.0. How do I get the two websites to run together in the same website using different versions of the .NET Framework?
View 2 Replies
Jan 6, 2011
I have this cenario:One server (machine) that have framework 3.5. And one site (website) developed for framework 3.5.This site can not be modified (rebuild in other FW version, or others changes).
I'll developer a new webapplication (WAP) in framework 4.0 (using microsoft visual web developer 2010).
As I know, the new webaplication work in the server, if I install framework 4.0 in the server.
My question is: Can I install the framework 4.0 on the machine that already have framework 3.5. And continue have sites working with other frameworks version?Or, should I develop my new webapplication in framawork 3.5 ?If I can install the framework 4.0, do I need make some specify configuration during the installation?
View 2 Replies
Jun 18, 2010
what is Features added in Framework 2.0 over the framework 1.1? i want in Details?
View 2 Replies
Feb 6, 2010
Difference between .net framework 2.0 and .net framework 2.0 with service pack 2
View 4 Replies
Apr 23, 2010
We are developping with .net framework 2.0 on our dev station and we deploy on web server and client desktop a 3 tiers smart client app. what is the impact of ugrading to .net framework 2.0 sp2 on our web servers WITHOUT upgrading on dev station and compilation servers?
View 2 Replies
Jan 5, 2011
I recently I have upgraded my web application from 1.1 to 3.5.
Now the problem is support DLLs used in this application still in 1.1 only.
There is no way to change it. While accessing from 3.5 it is not showing any error msg.
But not giving expected results.
Is there any way to run this particular assembly in 1.1 and other in 3.5 ?
View 1 Replies
Mar 26, 2011
I have an old Web application which is using net framework 2.0(build with vs2005), now i need to move the old Web application to production server which has .net framwork 3.5 installed. Do I need recompile the web application with vs2008 or i can just go head move the the Web application without recompiling, it will automatically work?
View 4 Replies
Apr 30, 2010
I am new in ASP.Net and C#.
I have used HttpContext.Current.Response.Redirect in a class file.
Is it right? or if any problem occur by using this?
View 2 Replies