How to write unit test for method returning complex object

I have mark calculating method

 public Mark getMark(int courseID, string studID, string forumID)
        {           
            int initialMark = 0;
            int finalMark = 0;
            int threshold = 40;
            string mark = "";
            int componentVal = 0;
            int componentTreshold = 40;

            InitStudentInfo(courseID, studID, forumID);

            foreach (var component in calc.CourseComponents)
            {
                //a lot of conditions like below calculating the mark 
                if (componentVal < componentTreshold)   
                {   
                    if (roundedMark >= threshold)    
                    {
                        roundedMark = 60;                  
                        mark = "F";
                    }
                } 

                //...
            }  

            CourseMark obj = new CourseMark();
            obj .mark = mark;
            obj .roundedMark = roundedMark;
            obj .credit = Convert.ToInt32(calc.Credit);
            obj .option = calc.option;
            obj .level = calc.level;

            return mmg;
        }

I need to write unit tests checking the final result obj .mark = mark only, but I don't know what arguments to pass inside the method and how to assign mark component variables for my test cases that are no parameters for this method. And how to get the resulting value obj .mark = mark for the unit test from the complex object returned by the method. Also, this method inside the class has interface references, how to fulfil them when calling the method from the unit test class which I got a reference from MVC interview questions and answers.

public class MarkService : IMarkService
    {
      
        public MarkService(ICourseService CourseService //and etc.

My test methods, for now, looks like

[TestClass]
    public class MarkTest
    {

        int initialMark = 0;
        int finalMark = 0;
        int threshold = 40;
        string mark = "";
        int componentVal = 0;           
        int componentTreshold = 40;


        [TestMethod]
        public void getMarkPass()
        {
            componentVal = 20;
            mark = "Pass";

            MarkService ms = MarkService(); //here is interface references problem
            ms.getMark(); //here I don't know how to pass arguments
        }
       
    }

1 Reply 1 reply marked as answer

JL Joshna Lingala Uthama Reddy Lingala Syncfusion Team March 8, 2022 04:31 PM UTC


Marked as answer
Loader.
Up arrow icon