Mvccontrib Test Helper And Verifying Http Post Routes And Parameters?

Jan 29, 2010

In my Asp.net MVC app, I have two methods on a controller, one for when the user first arrives on the view and then one when they submit the form on said view.

public ActionResult Foo() {}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Foo(string id, Account accountToFoo) {}

In the second action, there's a custom model binder that's assembling the account object that I'm acting on, though that's really not important. This all works fine in testing locally on a server.We try to be pretty good about writing unit tests to test all our different views are properly getting routed to, including those that are HTTP POST. To do so, we've been using mvccontrib's test helper.

Testing gets have been super simple

"~/account/foo/myusername".
Route().
ShouldMapTo<AccountController>(c => c.Foo("myusername"));

My question is in testing POST routes, how do I write the lambda that I would use to verify the post is receiving accurate values, similar to the GET test above?

For a POST, it looks something like:

"~/account/foo".
WithMethod(HttpVerbs.Post).
ShouldMapTo<AccountController>(a => something_something);

It's the something_something portion of my lambda that I'm having trouble with. Using arbitrary values doesn't work ("a => a.Foo(0, new Account()"). How would I specify the expected values as part of the test?EDIT I was hoping there was something akin to the way Moq has lambdas for statements such as foo.Setup(s => s.Foo(It.IsAny(), It.Is(i => i > 32)) and so on. Even I have to explicitly supply the values, that's workable--I just can't seem to grok the desired structure to pass those explicit values.

View 2 Replies


Similar Messages:

C# - Test A MVC2 Post Action With Validation When Using MvcContrib TestHelper?

Oct 12, 2010

I'm attempting to write a unit tests for an ASP.NET MVC 2 post action that takes a view model as its sole parameter. The view model is decorated with validation attributes such as [Required]. I'd like to test two scenarios. The first scenario is when a valid set of data is passed in (ie, all required properties have values) and a redirect to the list page is returned. The second scenario involves passing in invalid data (eg, when one or more of the Required properties are not set). In this case the same view is returned with error messages.The action signature is as follows:

[HttpPost]
public virtual ActionResult Create(NewsViewModel model)

The NewsViewModel class is as follows:

public class NewsViewModel
{
public Guid Id { get; set; }

[code]...

View 1 Replies

MVC :: Testing Routes With MvcContrib.TestHelper And MsUnit?

May 8, 2010

I'm trying to get my route tests going using MvcContrib.TestHelper and MsUnit. This is what I got: (Pretty much straight out of MVC 2 in Action, Chapter 26, Testing Practices. Just I *have* to use msUnit, while the book use nUnit)

[Code]....

However I get an error saying : MvcContribTestHelperSandbox.Tests.Controllers.RouteTests.Setup has wrong signature. The method should be marked .. [rest is just cut off...not nice!) When I tried runnig it without the setup, I get some error complaining about a wrong version of Rhino Mocks, which I'm not even using! (Intend to add a reference to Moq.dll when I need to do mocking)

View 3 Replies

The HTTP Verb POST Used To Access Path '/test.html' Is Not Allowed

Feb 5, 2010

Below is my code:

[code]....

The HTTP verb POST used to access path '/test.html' is not allowed

View 4 Replies

MVC :: Routes Uniqueness And SEO - Using Id And Name As Parameters In The Actions?

Feb 2, 2010

I am using the Default Route as follows:

routes.MapRouteLower("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" });

However, beacause of SEO I would also like to filter not by id but by name. Something like: Product/Show/RedBall instead of Product/Show/2345 And if RedBall is not unique what would be a good approach to do this? Maybe start using the following: Product/Show/2345/RedBall Using Id and Name as parameters in the actions?

View 2 Replies

Unit Test An UrlHelper Custom Helper Method?

Mar 10, 2011

I am using ASP.NET MVC 3 and NUnit. I am trying to write a unit to test one of my helper methods. Here it is:

public static class UrlHelperAssetExtensions
{
private static readonly string yuiBuildPath = "http://yui.yahooapis.com/2.8.2r1/build/";
public static string YuiResetFontsGridsStylesheet(this UrlHelper helper)
{
return helper.Content(yuiBuildPath + "reset-fonts-grids/reset-fonts-grids.css");
}
}

Here is my unit test:

[Test]
public void YuiResetFontsGridsStylesheet_should_return_stylesheet()
{
// Arrange
RequestContext requestContext = new RequestContext();
UrlHelper urlHelper = new UrlHelper(requestContext);
// Act
string actual = urlHelper.YuiResetFontsGridsStylesheet();
// Assert
string expected = yuiBuildPath + "reset-fonts-grids/reset-fonts-grids.css";
Assert.AreEqual(expected, actual);
}....................................

View 2 Replies

MVC :: RedirectResult Does A HTTP Get. Want The Function That Does Http Post?

Apr 14, 2010

RedirectResult is doing a HTTP Get. I want a redirect that does a Http POST

View 2 Replies

MVC :: Html DropDown Helper, GET And POST Data From 2 Tables?

Mar 18, 2011

i am really confuse about the html dropdown helper. i cant reli find one info which describe clearly about every overload and how to use them.currently i gone through a problem which i spent one day and still cant resolve it, really hope can get the help here since i need to make tis work for my final year project..i have a food table, and a foodtype table. each food will have a type, so the FoodTypeID is the FK for Food table.i want to have a create and edit page for Food. on the page, i want to provide a dropdownlist for user to select the food typethe FoodTypeName column that store the food type description is at the FoodType table, so i need to get the value of FoodTypeID based on user selection on the dropdown.I had tried for hourssss to do this bt i either get a compilation or cant save the new foodType selected in db

[Code]....

in fact, i not reli understand wat should be put inside the model => xxx and the dropdown helper overload, i am writing this based on the mvc tutorial

View 12 Replies

MVC :: Html Helper Data Grid - Sorting Columns Using Post?

Jul 22, 2010

I've been asked to take over a project that's filled with a bunch of bugs. It's currently using MVC 1.0 and there's a view with Html Helper Data Grid that uses a DataGridHelper class. I'm not that familiar with all of MVC yet, and I was wondering if it is possible to have the sorting of the columns be a POST event rather than a GET.

Currently when a user clicks a column to sort, the controller for the view calls the GET action method. This I guess is all fine and good, but the problem is that this view has a search form (which is a model in it's own right) with some text boxes and some drop down lists. When the GET action method is called, I lose all of the information in the search criteria. So when ever someone searches for something, but then wants to sort the results, it requries the db for all records (because all of the search criteria is cleared).

I guess there could be two solutions:

1) Is there a way to access the items in the search form / model in the GET portion of the action method? I've tried using ViewData or adding a parameter for the search form model, but (and maybe I'm doing it wrong) both return null.

2) Is there a way to make the sorting event of a column call the POST portion of the action method? That way I'll have the search form / model information and be able to sort the search result content instead of query for all records.

Also, there is a stored procedure that currently can handel all of possible search parameters. So, no matter what sort column is selected, or search parameters are entered, it can properly setup the sql query.

View 6 Replies

MVC :: How To Test Action Which Alters Http Response

Apr 27, 2010

How can I unit test an action which alters the Http Response object? I haven't been able to solve this. I can mock the object but can't see how I can check the value after the action runs. I set the Response.StatusCode when errors occur in ajax requests and need to check it has been set in the unit test. The only alternative I can see is to change all my actions to return json objects with a IsSuccessful flag or similar.

View 5 Replies

Basic Post To Test Web Service?

Jan 11, 2010

I am working on form, which send input to webservice via post and display result. It must be simple, and it works fine on localhost. But when I try to use it agains live I have error 500.

Here is my code:

WebRequest request = WebRequest.Create("http://localhost:3192/WebServices/Export.asmx/" + uxAction.SelectedValue);
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(uxRequest.Text);
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
request.ContentLength = data.Length;

[Code]....

View 1 Replies

C# - Pass Model Values As Javascript Function Parameters From Inside Mvc Html Helper Such A Html.Radio?

Jul 20, 2010

I think I need to drop in some escape characters, but I'm not quite sure where. Here is the javascript function I'm attempting to call:

function setData(associateValue, reviewDateValue) {
var associate = document.getElementById("Associate");
var reviewDate = document.getElementById("ReviewDate");
associate.value = associateValue;
reviewDate.value = reviewDateValue;
}

Here is the asp .net mvc line where I'm attempting to create a Radio button with a click event that calls the above function and passes data from the model as javascript parameter values.

<%= Html.RadioButton("Selected", item.Selected, new { onClick="setData('<%=item.Associate%>','<%=item.ReviewDate%>' )" } )%>

The above throws a bunch of compile issues and doesn't work. A call such as the following does call the javascript, but doesn't get the data from the model.

<%= Html.RadioButton("Selected", item.Selected, new { onClick="setData('item.Associate','item.ReviewDate' )" } )%>
<%= Html.RadioButton("Selected", item.Selected, new { onClick="setData('item.Associate','item.ReviewDate' )" } )%>
<% String functionCall = String.Format("setData('{0}','{1}')", Html.Encode(item.Associate), Html.Encode(item.ReviewDate )) ; %>
<%= Html.RadioButton("Selected", item.Selected, new { onClick=functionCall } )%>

View 2 Replies

DataSource Controls :: Full Text Search Test - Works Without Parameters

May 28, 2010

I'm new with all stored procs - but I have a very basic Full Text Search going on...I created a stored proc, and linked it up to my application using Linq to SQL. If I use Linq to SQL to call the stored proc without any parameters (hard coded search string) i get results back, as soon as I add a parameter - the result set is 0 for some reason.

The funny thing is that I run Profiler and execute both queries its trying to do - and it looks identical

Here is the working parameterless query:

[Code]....

and according to Profile this is what it looks like when its called as a stored proc

[Code]....

I get 2 results as expected.
Here is the NON-working stored proc that takes in 1 string parameter

[Code]....

and here is the query that gets called when profiling

[Code]....

declare @p4 int

View 3 Replies

MVC :: Routes.IgnoreRoute Not Ignoring Routes?

May 25, 2010

I'm trying to prevent direct file requests from being accepted, and have tried this:

[Code]....

None of these worked. I want the rule to exclude any file of any filename in any directory with a matching extension. How can this be done?

View 11 Replies

Why Parameters Are Encoded When Sent Through HTTP Request

Feb 17, 2011

I am curious to know the reasons why the HTTP request encodes(URLEncoded) all the parameters before sending it across the network in POST request.

View 1 Replies

Web Forms :: Get Response Value - From Http Post

Mar 23, 2011

What wrong with this code?

[Code]....

I would like the numeric values of the enum of Response.StatisCode

View 1 Replies

Sending HTTP Post That Sends XML?

Nov 11, 2010

I am trying to add Canada Post shipping information to my website. Canada Post has this link to a sample site showing what to send. [URL] I have worked with Canada Post support and they have no sample code for C# or Visual Studio. Can some get me started on how to send the above information from a button click event?

View 1 Replies

C# - HTTP Post Without Redirecting User?

Aug 12, 2010

I'm trying to push data to a form in ASPX, but I dont want to the user to be taken to the post page.

I.e

When a user registers on the site I need to push some data to a form and submit the form without the user being redirected.

View 1 Replies

WCF / ASMX :: HTTP Post To Web Service?

Sep 16, 2010

have a web service that I'm trying to consume from a console app through http post. I receive a 500 exception error from the xmlstring parm being passed to the web service. Add the following to web.config of the web service. Although it is not working in debug in vs 2008 as well.

[Code]....

[Code]....

[Code]....

the host file is setup for the web service as well.

[Code]....

View 6 Replies

Web Forms :: Post Url With Http Webrequest?

Sep 9, 2010

I have a remote website and it requires login id and password to enter and i want to enter into that website with httpwebrequest. How it is possible?

View 1 Replies

C# - Post A File Over HTTP Via Public API?

Jan 7, 2010

I have a web application. I am using C#. I have existing methods in my API for various things but all only submit and return bool/int/strings.

All of my API methods have the directive System.ServiceModel.OperationContract

All the parameters are of System.Runtime.Serialization.DataMember

I would like to be able to receive a posted file over HTTP. All I've been finding is people attempting to save a HttpPostedFile after submitting it in a form.

EDIT: this will be called from an iPhone application. Not via the browser on the iPhone Basically, I would like to do this: [URL]

View 2 Replies

Security :: Sender URL For HTTP POST?

Jul 19, 2010

I am working on a application which will accept the data in the post request.I will surely implement the data encryption to make sure that communication is secured. But my concern is, any body who knows the URL will be able to send the POST data request to my application, can I restrict the request from once specific IP address/URL.From a Request object can I find out which application/HOSTname/URL has sent this request. I looked at RefererURL but it can be populated and cannot be used. Is there any other field/properly which will tell me about the party who has sent this request.I want to make sure that I process request received from one specific URL/IP and want to ignore all others.

View 5 Replies

Altering The MVC 2 ActionResult On HTTP Post?

May 2, 2010

I want to do some processing on a attribute before returning the view. If I set the appModel.Markup returned in the HttpPost ActionResult method below to "modified" it still says "original" on the form. Why cant I modify my attribute in a HttpGet ActionResult method?

[HttpGet]
public ActionResult Index()
{
return View(new MyModel
{
Markup = "original"
});
}
[HttpPost]
public ActionResult Index(MyModel appModel)
{
return View(new MyModel
{
Markup = "modified"
});
}

View 1 Replies

Receiving A Message That Was Sent Using HTTP POST?

Sep 1, 2010

I'm somewhere between a beginner and intermediate asp.net and c# user who is looking to receive a message/variable and then URL decode it. The message is being sent via HTTP post. I'm not sure exactly how to get started, and am looking for a tutorial but haven't been able to find one.

View 4 Replies

Web Forms :: XML HTTP Post -- Capture Into DB

Nov 10, 2010

I need to offer a way for a client to send me XML (list of contacts). I'd like to do this using HTTP post. The page needs to capture that XML and place the data into my database (FirstName, LastName, Address...).

View 1 Replies







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