1

Please tell me how to get the value by not changing the controller action.

Controller
     [HttpPost]
        public JsonResult A_Action_In_Controller(Guid ID)
        {
            var operationConfirmation = _repository.DoSomethingInDB(emailID);

            return Json(new { operationConfirmation }, JsonRequestBehavior.AllowGet);
        }
Test Method
    [TestMethod]
        public void DoSomethingInDB_SendOperationConfirmationToTheUI()
        {... 
                var expected = "Successfully Completed";

            var target = controller.A_Action_In_Controller(obj1.Id);

            Assert.AreEqual(expected, target.Data);

        }

Error

Assert.AreEqual failed. Expected:<Successfully Completed (System.String)>. Actual:<{ operationConfirmation = Successfully

Completed } (<>f__AnonymousType2`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]])>.

Please tell me how to write something like Assert.AreEqual(expected, target.Data.operationConfirmation);

instead of what I am having now, i dont want to change my controller code

Assert.AreEqual(expected, target.Data);

2 Answers 2

0

Controller is changed to

Controller
 [HttpPost]
    public JsonResult A_Action_In_Controller(Guid ID)
    {
        var operationConfirmation = _repository.DoSomethingInDB(emailID);

        return Json(operationConfirmation, JsonRequestBehavior.AllowGet);
    }

Then test method works well

  [TestMethod]
    public void DoSomethingInDB_SendOperationConfirmationToTheUI()
    {... var expected = "Operation failed";         


        var target = controller.A_Action_In_Controller(obj1.Id);

        Assert.AreEqual(expected, target.Data);
0

You need to deserialize the JSON string, the below uses the JavaScriptSerializer class:

First, name your confirmation in your A_Action_In_Controller method, like so:

return Json(new { confirmation = operationConfirmation }, JsonRequestBehavior.AllowGet);

Then do this in your test method:

var js = new JavaScriptSerializer();
var deserializedTarget = (object[])js.DeserializeObject(target.Data.ToString());
var result = (string)deserializedTarget["confirmation"];

Then you can do:

Assert.AreEqual(expected, result);
10
  • hi I am getting compilation error by using your solution and after modifying it then exception is occurring e.g. Invalid object passed in, ':' or '}' expected. (16): { confirmation = Successfully Completed }
    – Shantu
    Commented Jan 21, 2013 at 12:42
  • i have changed it to var deserializedTarget = (object[])js.DeserializeObject(target.Data.ToString());
    – Shantu
    Commented Jan 21, 2013 at 12:43
  • Wrap Successfully Completed into quotes, like { confirmation = "Successfully Completed" } Commented Jan 21, 2013 at 12:43
  • Do you mean return Json(new { confirmation = "\""+operationConfirmation+"\""}, JsonRequestBehavior.AllowGet); i dont think this is a good idea
    – Shantu
    Commented Jan 21, 2013 at 12:51
  • Ah sorry no, I thought you had that string hard coded without quotes. Is it working? Commented Jan 21, 2013 at 12:52

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.