AnthonySteele / MvcRouteTester

A library for unit testing ASP MVC route tables for both Web and API Routes
Apache License 2.0
105 stars 43 forks source link

List example #24

Closed bapti closed 9 years ago

bapti commented 11 years ago

Hi,

It would be useful to have an example in the tests of how to test a method that takes a list

public class RecordController : Controller
{
    [HttpPost]
    public ActionResult Index(List<Record> records)
    {
        //Logic
    }
}

I'm working on it now building the list into a query string but it would be useful if "withbody" would accept json perhaps?

[Fact]
public void RecordsRoute()
{
    var records = new List<Record>
    {
        new Record { Id = "TestId", RecordId = "TestRecordId" }
    };

    var body = "[0].Id=TestId&[0].RecordMetadataId=TestRecordMetadataId";

    Routes
        .ShouldMap("/record")
        .WithBody(body)
        .To<RecordController>(x => x.Index(records));
}

Or something like this would be good

 var body = String.Format("records='{0}'", records.ToJson());

Or even

 Routes
        .ShouldMap("/record")
        .WithJsonBody(records.ToJson())
        .To<RecordController>(x => x.Index(records)); 
AnthonySteele commented 11 years ago

Right now it only supports post body as form-url encoded. WithJsonBody(string body) is something that I will have a look at soon.

AnthonySteele commented 11 years ago

I have started to have a look at the json body parsing. The easiest way is to take a dependency on Newtonsoft.Json via nuget. This shouldn't be a problem, right?

bapti commented 11 years ago

Fine for me, that's the library that MVC uses for its default json parser I believe.

On Mon, Oct 28, 2013 at 2:11 PM, Anthony Steele notifications@github.comwrote:

I have started to have a look at the json body parsing. The easiest way is to take a dependency on Newtonsoft.Json via nuget. This shouldn't be a problem, right?

— Reply to this email directly or view it on GitHubhttps://github.com/AnthonySteele/MvcRouteTester/issues/24#issuecomment-27214297 .

bapti commented 11 years ago

Thanks again for the great library, it's been a brilliant help for us preventing regressions.

On Mon, Oct 28, 2013 at 2:46 PM, Neil Crawford neil.crawford@gmail.comwrote:

Fine for me, that's the library that MVC uses for its default json parser I believe.

On Mon, Oct 28, 2013 at 2:11 PM, Anthony Steele notifications@github.comwrote:

I have started to have a look at the json body parsing. The easiest way is to take a dependency on Newtonsoft.Json via nuget. This shouldn't be a problem, right?

— Reply to this email directly or view it on GitHubhttps://github.com/AnthonySteele/MvcRouteTester/issues/24#issuecomment-27214297 .

bapti commented 10 years ago

Hi, I was just wondering if you added this in the end?

AnthonySteele commented 10 years ago

Sorry, I got caught up in other things. And getting this lib to work on ASP MVC 5.1/WebApi 2.1 is the enxt step.

AnthonySteele commented 10 years ago

The json body parsing is in the code now. Lists aren't working though. I have a couple of failing test and will look at how to do this; it doesn't look like a trival thing though.

alexjamesbrown commented 10 years ago

@anthony

Is that on a branch anywhere?

AnthonySteele commented 10 years ago

I have made a ListTests branch https://github.com/AnthonySteele/MvcRouteTester/tree/ListTests