mercredi 3 février 2016

How to unit test an Action method that takes Array of an Object and returns JsonResult in ASP.Net MVC

I am trying to write a unit test in Visual C# Unit Test project.

I pass an empty Array of Frame class and that returns a JSON object.

    [HttpPost]
    public JsonResult SubmitBowlingScore(Frame[] frames)
    {
        int totalScore= 0;
        var objScore = new EngineService();

        for (int i = 0; i < frames.Length; i++)
        {
            Boolean wasSpare = false;

            if (i > 0 && objScore.IsSpare(frames[i-1]))
            {
                wasSpare = true;
            }
            totalScore += objScore.CalculateScore(frames[i], wasSpare);
        }

        return Json("{\"score\":"+ totalScore + "}");
    }

Hoping to test with the following entry: But no idea how!!!

 [{""1stRoll"":2,""2ndRoll"":2 ,""3rdRoll"":0},
  {""1stRoll"":4,""2ndRoll"":8 ,""3rdRoll"":0},
  {""1stRoll"":6,""2ndRoll"":2 ,""3rdRoll"":0}];

Any help/idea/suggestion would be appreciated for the following unit test. How SubmitBowlingScore() would take the Frame [] data as a parameter?

    [TestMethod]
    public void SubmitBowlingScore()
    {
        //Arrange
        HomeController controller = new HomeController();
        //Act
        JsonResult result = controller.SubmitBowlingScore(**What goes here???**) as JsonResult;

        //Assert
        Assert.IsNotNull(JsonResult, "No JsonResult returned from action method.");
        Assert.AreEqual(@"{[{""1stRoll"":""2"",""2ndRoll"":2 ,""3rdRoll"":0},{""1stRoll"":""2"",""2ndRoll"":8 ,""3rdRoll"":0},{""1stRoll"":""6"",""2ndRoll"":2 ,""3rdRoll"":0}],""Count"":3,""Success"":true}",
               result.Data.ToString());
    }

Aucun commentaire:

Enregistrer un commentaire