Standalone Mocking In .Net 4.0 MVC?

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


Similar Messages:

Installation :: Cannot Download 4 Standalone Or Web Installer

Oct 26, 2010

Can not download anything from Microsoft download, asp .net 4, sharepoint services 3.0, SQL server Express 8.0 and services pack

View 2 Replies

C# - Can The Microsoft TextTransform Utility Be Used Standalone

Dec 22, 2010

Can the Microsoft TextTransform utility be used standalone? Since VS2010 is it still true as I can read: [URL] Preprocessed templates to allow embedding of template-based generation in arbitrary applications without a runtime T4 dependency. There is a new custom tool, new service APIs and new engine APIs to support this feature.

View 2 Replies

Security :: Standalone Stored Procedure To Verify Credentials In Aspnet Database?

Apr 12, 2010

I have a requirement to develop an application in Excel VBA to talk to an MSSQL database.The client wants a login facility for a number of named users - I am going to setup these users in the aspnet tables in my database (aspnet_users, aspnet_membership etc).There is no facility in VBA to do automatic verification of a users credentials like there is in ASP.Net so I have developed a simple login form for the user to enter their username and password and send these to a stored procedure in the database. I am looking for a stored procedure that implements the ASP.Net hashing routine to convert the user supplied password into the same format as the database stored password so I can compare and return a true or false.

View 1 Replies

Developing Rich Web Applications For Mobile Platforms (against Platform Specific Standalone Apps)?

Oct 18, 2010

I am new to mobile app development and I dont want restrict myself to one platform. Some days ago I read an article that stressed on the fact that soon web apps running on mobile browsers may replace dedicated apps (at least for non-game apps). I want to develop a social networking web app that any phone can run via the browser. These are the things I would like expert comments on:

Resources/Guidelines for developing great UIs for rich mobile web apps (HTML5?, existing APIs/Frameworks I should know)The best ways to increase performance(a reusable native simple browser app per platform that caches, provides a uniform interface to the web apps,access to sensors, APIs, features like drag drop etc for that platform.is there already one?) How do I test the apps for various major platforms and across various devices (small/large screens, touch phones etc)Best ways to manage multiple device compatibility.. (how to best handle non-uniformity of functionality on various devices/browsers on the server side using ASP.NET)

View 1 Replies

Architecture :: Looking For Best Mocking Tool ?

Feb 26, 2010

I am looking for some guidance
\
-Rhino.Mocks
-Moq
-NMock
-TypeMock (not free)

View 1 Replies

MVC :: RhinoMock + Mocking The Httpcookie?

Jun 1, 2010

I have a cookie which my controller uses.

How should i mock the cookie?

View 3 Replies

Mocking Advantages Over NUint?

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

Mocking User.Identity In .NET MVC?

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

MVC :: Feedback On The Most Optimal Toolsets For TDD, Mocking, And IOC?

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

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

Mocking HttpSessionState In .net For Nunit Testing

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

Soapui Mocking Services Which Return Json

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

MVC :: Unit Testing + Mocking Session Variable?

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

Mvc Rhino Mocks Mocking Httprequest Values?

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

Unit Testing / Mocking HttpContext Without Using Any Mock Framework

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

C# - Mocking Method Call With Out Param And No Return Value In Rhino Mocks?

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

Unit Testing - Mocking HttpRequest In .NET 4.0 / HttpRequest Is Sealed With No Interface

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







Copyrights 2005-15 www.BigResource.com, All rights reserved