2

How can I unit test if my JSON input is being deserialized? I am trying to deserialize a JSON input, calculate and then serialize. But I don't know how to check if my input is being deserialized. So I am writing unit test to validate.

NOTE: AlphaCalcParam ParseParameter is a private method. This is where I am getting an error.

Unit Test

   [TestMethod()]
            public void ParseParameterTest()
            {
                Algo.Alpha.AlphaCalculator calc = new Alpha.AlphaCalculator();

                string test_input = File.ReadAllText(@"..\..\..\case\Alpha Example Input.json");
                string expected = File.ReadAllText(@"..\..\..\case\Alpha Example DOutput.json");
                string res = calc.AlphaCalcParam(test_input);

                res == expected
                Assert.Fail();
            }

Logic

 public string Calculation(string json_param)
        {
            try
            {
                AlphaCalcParam param = ParseParameter(json_param);
                AlphaCalcResults result = CalculateAlpha(param);

                return JsonConvert.SerializeObject(result);
            }
            catch (Exception e)
            {
                return "Failed in Alpha Calculation!. " + e.Message;
            }
        }

...some more code.. below is what i want to test...

 private AlphaCalcParam ParseParameter(string json_param)
        {
            try
            {
                return JsonConvert.DeserializeObject<AlphaCalcParam>(json_param);
            }
            catch (Exception ex)
            {
                throw new Exception("The input json string format is wrong for Alpha Calculation!. " + ex.Message);
            }
        }
1
  • 5
    Seriously, don't understand with all the downvotes. New to C#. Trying to learn. Help me improve. That is what I thought stackoverflow is for. Commented Feb 17, 2017 at 18:10

1 Answer 1

1

Your unit test right now is always Asserting to a failure. What you need to do is test the expected value of res.

 [TestMethod()]
        public void ParseParameterTest()
        {
            Algo.Alpha.AlphaCalculator calc = new Alpha.AlphaCalculator();

            string test_input = File.ReadAllText(@"..\..\..\case\Alpha Example Input.json");
            string expected = File.ReadAllText(@"..\..\..\case\Alpha Example DOutput.json");
            string res = calc.AlphaCalcParam(test_input);

            Assert.AreEqual(expected, res);
        }

If res isn't the expected value, the unit test will fail.

Take a look at the Assert Class documenation for all available methods of testing.

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

2 Comments

@dpimenete thanks but AlphaCalcParam is a private method.
Well the logic of unit testing is what you want. For testing you may have to make the AlphaCalcParam method internal or public, not private. Otherwise a new accessor function could be used.

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.