3

The controller action being tested:

    [AuthorizeUser]
    [HttpPost]
    [ValidateJsonAntiForgeryToken]
    public ActionResult EventDetails(int eventId)
    {
        string details = this._eventDataProvider.GetById(eventId).Comments;

        if (string.IsNullOrEmpty(details))
            details = "This location has not entered any comments or further details for this event.";

        return Json(new
        {
            details = details
        });
    }

Test code for the controller: wondering what I need to do to test the Json being returned from the controller:

    [TestMethod]
    public void DetailsAreReturned()
    {
        // Arrange
        eventsController = new EventsController(eventDataProvider.Object, playerEventDataProvider.Object, userDataProvider.Object,
                                                tokenAuthent.Object, dataContext.Object, customerLocationDataProvider.Object);

        eventDataProvider.Setup(x => x.GetById(1)).Returns(new Event() { Comments = "test" });

        // Act
        JsonResult result = (JsonResult) eventsController.EventDetails(1);

        // Assert
        Assert.IsNotNull(result.Data);

        Assert.AreEqual(??, result);
    }
0

1 Answer 1

9

I have to give credit to this post first: How do I iterate over the properties of an anonymous object in C#?

var result = new JsonResult{ Data = new {details = "This location has not entered any comments or further details for this event."}};

var det = result.Data.GetType().GetProperty("details", BindingFlags.Instance | BindingFlags.Public);

var dataVal = det.GetValue(result.Data, null);

Hope this helps or at least gives you a jumping point.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.