Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this code in C# mvc Controller:

[HttpPost]
    public ActionResult Delete(string runId)
    {
        if (runId == "" || runId == null)
        {
            return this.Json(new { error = "Null or empty params" });
        }
        try
        {
            int userId = (int)Session["UserId"];
            int run = Convert.ToInt32(runId);

            CloudMgr cloud = new CloudMgr(Session);
            cloud.DeleteRun(userId, run);

            return this.Json(new { success = true });
        }
        catch (Exception ex)
        {
            return this.Json(new { error = ex.ToString() });
        }
    }

How I can access my Json "error" field in a ControllerTest to check if it is null or not?

[TestMethod]
    public void DeleteWrongParam()
    {
        WhatIfController controller = new WhatIfController();
        controller.ControllerContext = 
        TestUtils.CreateMockSessionControllerContext().Object as ControllerContext;

        JsonResult result = controller.DeleteWhatIf(null) as JsonResult;

Assert.IsNotNull(result.Data.error); is what I would like to do. Any Ideas? Thanks.

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

you can use like this , Result will be expected object definition . So in case od success , your success flag will be TRUE otherwise false and if false then you should expect that error property is updated with the error message.

        JsonResult jsonResult = oemController.List() as JsonResult;
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Result result = serializer.Deserialize<Result>(serializer.Serialize(jsonResult.Data));

        public class Result 
        {
            public bool success ;
            public string error;
        }
share|improve this answer
    
What is the namespace for Result class? Thanks. –  juanchoelx Jun 21 '13 at 10:13
    
@juanchoelx Result is your own defined class like this class Result { public bool success ; public string error } –  Devesh Jun 21 '13 at 10:35
    
@juanchoelx I updated my code in my answer. –  Devesh Jun 21 '13 at 10:40
    
With this solution you need as many "Result" classes as different JSon results you have. If you build a new Action which returns two integers instead of success/error fields, you need to build a new class. –  J punto Marcos Jun 21 '13 at 11:26
add comment

JavaScriptSerializer is good for string and static type. Here you created anonymous type as Json(new { success = true }). This case, you had better used dynamic type.

JsonResult result = controller.DeleteWhatIf(null) as JsonResult;
dynamic dresult = result.Data;
Assert.IsTrue(dresult.succes);

You need to import Microsoft.CSharp dll to test project. If test and your controller are different assembly, you need to make friend assembly from controller assembly.

[assembly: InternalsVisibleTo("testproject assembly name")]

share|improve this answer
    
+1 for the InternalsVisibleTo tip. I tried doing this without having that assembly attribute it place and couldn't figure out why it was blowing up at first. –  Landon Poch Feb 28 at 17:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.