简体   繁体   English

这段代码的单元测试是什么?

[英]What will be the unit testing for this code?

What will be the unit testing code for the given code below... Got stuck for a while so please help me find the solution...下面给定代码的单元测试代码是什么...卡了一段时间,所以请帮我找到解决方案...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoAppCore
{
    public class Program
    {
        public static string CacluateGrade(float mark)
        {
            string grade = "";
            if (mark < 40)
                grade = "FAIL";
            else if (mark >= 40)
                grade = "PASS";
            return grade;
        }
    }
}

First, I would suggest you to simplify your code:首先,我建议您简化代码:

public static string CacluateGrade(float mark)
{
    if (mark < 40)
        return "FAIL";

    return "PASS";
}

Then, you can make tests for two conditions: mark less than 40, and mark greater than or equals to 40.然后,您可以对两个条件进行测试: mark小于 40 和mark大于等于 40。

[Test]
public void CalcualteGrade_With39_ReturnsFAIL()
{
    var actual = Program.CalculateGrade(39);

    Assert.Equal("FAIL", actual);
}

[Test]
public void CalcualteGrade_With40_ReturnsPASS()
{
    var actual = Program.CalculateGrade(40);

    Assert.Equal("PASS", actual);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM