DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. ASP.net MVC Action Methods: Testing Against Anonymous Return Types

ASP.net MVC Action Methods: Testing Against Anonymous Return Types

Juri Strumpflohner user avatar by
Juri Strumpflohner
·
Jan. 25, 13 · Interview
Like (0)
Save
Tweet
Share
4.05K Views

Join the DZone community and get the full member experience.

Join For Free

I don’t use dynamic types regularly, but there are always again situations when they come in quite handy. Here is one example of testing an MVC controller’s return value.

You might encounter the situation where you want to test the returned value of an ASP.net MVC controller. For instance you might have the following action method:

public JsonResult GetById(long id)
{
    var person = personRepository.GetById(id);
    return Json(person, JsonRequestBehavior.AllowGet);
}

Testing an Action Method

Then a corresponding simple test method could might look like this

[TestMethod]
public void ShouldReturnAPersonWhenPassingAValidId()
{
    //Act
    var controllerData = personController.GetById(3);
    var person = controllerData.Data as Person;

    //Assert
    Assert.AreEqual("Juri", person.Firstname);
}

When invoking the controller’s action method you get the returned ActionResult. The actual data of interest is wrapped in its according Data property.

Testing against Anonymous Types

It gets more interesting when you return anonymous types as in this dummy example:

public JsonResult CalculateValue()
{
    return Json(new { Sum = 10 }, JsonRequestBehavior.AllowGet);
}

How do you verify that the returned object’s property Sum contains the value 10?? Reflection would be one possibility, but with the dynamic types it’s even easier.

But caution, because anonymous types are internal, you need to add the InternalsVisibleTo attribute on the tested assembly, s.t. the test project can access its internal objects. Assume you have a project “AspMvcFrontEnd” and a corresponding test project called “AspMvcFrontEnd.Tests” and that the project name corresponds to the produced assembly name. Then you would have to add the following line to theAssemblyInfo.cs of the assembly you’re testing, that is the one hosting the controllers:

[assembly: InternalsVisibleTo("AspMvcFrontEnd.Tests")]

The corresonding test can then be written in quite a simple form like this (note the definition of the dynamic type):

[TestMethod]
public void ShouldReturnAnAnonymousTypeWithSumEq10()
{
    //Act
    dynamic obj = controller.CalculateValue().Data;

    //Assert
    Assert.AreEqual(10, obj.Sum, "the sum should be eq to 10");
}

Quite an elegant approach in contrast to a solution by using reflection.

ASP.NET MVC Testing Test method Assembly (CLI) Property (programming) Object (computer science) Data (computing) Form (document) Attribute (computing) IT

Published at DZone with permission of Juri Strumpflohner. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Event Driven 2.0
  • Stream Processing vs. Batch Processing: What to Know
  • Quick Pattern-Matching Queries in PostgreSQL and YugabyteDB
  • Bye-Bye, Regular Dev [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: