asp.net-mvc


NSubstitute: Mocking the request, response object inside a MVC/Web Api Controller?


I am trying to find how to mock both the Request and Response objects that are available inside a controller from MVC / Web Api.
Is this possible, I am not injecting the Request and Response objects, these are available because the controllers inherit from ApiController or Controller.
Does anyone have some good examples for getting access to these through nsubstitute ?
Also what about the other objects like context ?
You do not have to mock them.
Since they have read/write properties, all you have to do is to create them. I explain it a bit more in our book and we have a class that allows you to do this:
var orderController = ControllerContextSetup
.Of(() => new OrderController(mockOrderService.Object))
.WithDefaultConfig()
.WithDefaultRoute()
.Requesting(url)
.WithRouteData(new {controller="Order"})
.Build<OrderController>();
I am sharing the code here so it can be used:
public class ControllerContextSetup
{
private const string DefaultApiName = "DefaultApi";
private readonly ApiController _controller;
private HttpRouteData _httpRouteData;
private ControllerContextSetup(ApiController controller)
{
_controller = controller;
_controller.Request = new HttpRequestMessage();
}
public static ControllerContextSetup Of<T>(Func<T> factory)
where T : ApiController
{
return new ControllerContextSetup(factory());
}
public static ControllerContextSetup Of<T>()
where T : ApiController, new()
{
return new ControllerContextSetup(new T());
}
public ControllerContextSetup WithDefaultConfig()
{
_controller.Configuration = new HttpConfiguration();
_controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = _controller.Configuration;
return this;
}
public ControllerContextSetup WithConfig(HttpConfiguration configuration)
{
_controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
_controller.Configuration = configuration;
return this;
}
public ControllerContextSetup Requesting(string uriString)
{
Uri uri = null;
bool success = Uri.TryCreate(uriString, UriKind.Relative, out uri);
if (success)
return Requesting(uri);
success = Uri.TryCreate(uriString, UriKind.Absolute, out uri);
if(success)
return Requesting(uri);
return Requesting(new Uri(uriString));
}
public ControllerContextSetup Requesting(Uri uri)
{
_controller.Request.RequestUri = uri;
return this;
}
public ControllerContextSetup WithDefaultRoute()
{
_controller.Configuration.Routes.MapHttpRoute(
name: DefaultApiName,
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
return this;
}
public ControllerContextSetup WithRoute(string name, string routeTemplate)
{
_controller.Configuration.Routes.MapHttpRoute(name, routeTemplate);
return this;
}
public ControllerContextSetup WithRoute(string name, string routeTemplate, object defaults)
{
_controller.Configuration.Routes.MapHttpRoute(name, routeTemplate, defaults);
return this;
}
public ControllerContextSetup WithRoute(string name, string routeTemplate, object defaults, object constraints)
{
_controller.Configuration.Routes.MapHttpRoute(name, routeTemplate, defaults, constraints);
return this;
}
public ControllerContextSetup WithRoute(string name, IHttpRoute route)
{
_controller.Configuration.Routes.Add(name, route);
return this;
}
/// <summary>
/// Uses default route
/// </summary>
/// <param name="routeValues"></param>
/// <returns></returns>
public ControllerContextSetup WithRouteData(object routeValues)
{
return WithRouteData(new HttpRouteValueDictionary(routeValues));
}
/// <summary>
/// Uses default route
/// </summary>
/// <param name="routeValues"></param>
/// <returns></returns>
public ControllerContextSetup WithRouteData(HttpRouteValueDictionary routeValues)
{
var route = _controller.Configuration.Routes[DefaultApiName];
_httpRouteData = new HttpRouteData(route, routeValues);
_controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = _httpRouteData;
return this;
}
public ControllerContextSetup WithMethod(HttpMethod method)
{
_controller.Request.Method = method;
return this;
}
public ApiController Build()
{
_controller.ControllerContext =
new HttpControllerContext(_controller.Configuration,
_httpRouteData ?? new HttpRouteData(_controller.Configuration.Routes.FirstOrDefault()) ,
_controller.Request);
return _controller;
}
public T Build<T>()
where T : ApiController
{
return (T) Build();
}
}

Related Links

How do you secure static (HTML/css) content on an azure webapp virtual directory?
Scientific value is displaying ,while export data from GridView in MVC
MVC View Does Not Contain Expected Data
Sitecore / ASP.NET Request Validation Won't Disable
Cross Domain Resource Sharing
Execution permission cannot be acquired - FileLoadException: Could not load file or assembly 'Microsoft.VisualStudio.Web.PageInspector.Runtime
how to accelerate MVC WebAPI response time [closed]
How to use rotativa Print PDF in MVC
Sitecore rendering not found intermittently
Customizing Aurelia to use .cshtml
ViewResult class result not work when i submit a form method post for redirect to another view
using IIS self signed cert, but form data is still plain text
Passing parameters to Azure Active Directory authentication
How can I log a messages to a Oracle 11g database with Nlog in ASP.NET VNEXT (MVC 6)
ASP MVC load new view on selected item
$.getJSON Not working in deployment server

Categories

HOME
graph
f#
apacheds
word-vba
geolocation
jqgrid
normalization
mocha
httpbuilder-ng
google-analytics-api
cross-domain
akka.net
pie-chart
threadpool
command-line-interface
vulkan
plots.jl
silk-performer
servlet-filters
jruby
jet
cadvisor
confidence-interval
hawq
graphql-dotnet
feedly
popupwindow
bluebird
double-click-advertising
draggable
extraction
mask
transpose
joe-editor
otroslogviewer
es6-promise
xargs
nscalendar
sql-insert
notimplementedexception
database-cleaner
soql
mspgcc
maven-versions-plugin
ice
playback
getelementbyid
mapr
jeditorpane
ubuntu-15.04
struts1
pygments
sql-server-2016-express
namenode
airplay
tag-helpers
membership
uac
par
session-replication
wt
heroku-toolbelt
node-orm2
sketching
gem
data-science-studio
mkpolyline
horizontalscrollview
tortoisecvs
physx
express-jwt
populate
blazeds
pydatalog
preon
dfsort
xcache
asp.net-authentication
gigaspaces
makumba
datakey
asp.net-session
android-actionmode
time-limiting
textboxlist
xmltextwriter
measure
sloc
docking

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App