Testing JSON Results from MVC 4 C # Controller

So, I have a controller that returns json in my views, which I need to check. I tried using reflection with a dynamic data type to access the helper property of the list, but I still get something similar to "failure" errors. Basically, I have a list in the list that I want to get and check, but I can’t access it. Has anyone tested json from their controller before in MVC4 and have some advice?

the code:

// arrange
        var repositoryMock = new Mock<IEdwConsoleRepository>();
        var date = -1;
        var fromDate = DateTime.Today.AddDays(date);
        EtlTableHistory[] tableHistories =
            {
                new EtlTableHistory
                    {
                        Table = new Table(),
                        RelatedStatus = new BatchStatus(),
                        BatchId = 1
                    }
            };
        EtlBatchHistory[] batchHistories = { new EtlBatchHistory
                                             {
                                                 Status = new BatchStatus(),
                                                 TableHistories = tableHistories
                                             } };

        repositoryMock.Setup(repository => repository.GetBatchHistories(fromDate)).Returns((IEnumerable<EtlBatchHistory>)batchHistories);
        var controller = new EdwController(new Mock<IEdwSecurityService>().Object, repositoryMock.Object);

        // act
        ActionResult result = controller.BatchHistories(1);

        // assert
        Assert.IsInstanceOfType(result, typeof(JsonResult), "Result type was incorrect");
        var jsonResult = (JsonResult)result;
        var resultData = (dynamic)jsonResult.Data;
        var returnedHistories = resultData.GetType().GetProperty("batchHistories").GetValue(resultData, null);

        var returnedTableHistoriesType = returnedHistories.GetType();
        Assert.AreEqual(1, returnedTableHistoriesType.GetProperty("Count").GetValue(returnedHistories, null), "Wrong number of logs in result data");
+4
source share
2 answers

Here is an example:

Controller:

    [HttpPost]
    public JsonResult AddNewImage(string buildingID, string desc)
    {
        ReturnArgs r = new ReturnArgs();

        if (Request.Files.Count == 0)
        {
            r.Status = 102;
            r.Message = "Oops! That image did not seem to make it!";
            return Json(r);
        }
        if (!repo.BuildingExists(buildingID))
        {
            r.Status = 101;
            r.Message = "Oops! That building can't be found!";
            return Json(r);
        }

        SaveImages(buildingID, desc);
        r.Status = 200;
        r.Message = repo.GetBuildingByID(buildingID).images.Last().ImageID;

        return Json(r);
    }

    public class ReturnArgs
    {
        public int Status { get; set; }
        public string Message { get; set; }
    }

Test:

    [TestMethod]
    public void AddNewImage_Returns_Error_On_No_File()
    {
        // Arrange
        ExtendedBuilding bld = repo.GetBuildings()[0];
        string ID = bld.Id;

        var fakeContext = new Mock<HttpContextBase>();
        var fakeRequest = new Mock<HttpRequestBase>();

        fakeContext.Setup(cont => cont.Request).Returns(fakeRequest.Object);
        fakeRequest.Setup(req => req.Files.Count).Returns(0);

        BuildingController noFileController = new BuildingController(repo);
        noFileController.ControllerContext = new ControllerContext(fakeContext.Object, new System.Web.Routing.RouteData(), noFileController);

        // Act
        var result = noFileController.AddNewImage(ID, "empty");

        ReturnArgs data = (ReturnArgs)(result as JsonResult).Data;

        // Assert
        Assert.IsTrue(data.Status == 102);
    }

In your example, it seems to me that the problem is here:

var resultData = (dynamic)jsonResult.Data;
var returnedHistories = resultData.GetType().GetProperty("batchHistories").GetValue(resultData, null);

resultData , . , - :

List<String> list = repo.GetList();
return Json(list);

resultData :

List<String>

, Json (obj).

+3

Json ,

:

dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

var propertyValue = obj.MyProperty; //Ask for the right property

Json Nuget Json.Net.

0

All Articles