ExpectException属性无法显示预期的结果C#



我创建了一个类库。

我创建了单元测试项目来测试一个类。

我经历了一个名为

的特定课程
  " ExpectedExceptionAttribute Class "

我试图实施它。但是,如果我启用属性,我的代码总是显示失败。即使数据正确。

类:

public class SampleTestClass
{
    public double CheckValidAmount(object Name , double amount)
    {
        try
        {
            if (amount == 1.0 && Name.ToString() == "RamKumar")
                return 10.0 - amount;
            else
                return amount;
        }
        catch (NullReferenceException ex)
        {
            return amount;
        }
    }
}

单元测试通行证:

 [TestClass]
 public class SampleTester
 {
    [TestMethod]
    public void CheckValidAmount()
    {
        SampleTestClass sp = new SampleTestClass();
        double dd = sp.CheckValidAmount("RamKumar", 1.0);
        Assert.AreEqual(dd, 9.0);
    }
}

失败:

[TestClass]
public class SampleTester
{
    [TestMethod]
    [ExpectedException(typeof(NullReferenceException))]
    public void CheckValidAmount()
    {
        SampleTestClass sp = new SampleTestClass();
        double dd = sp.CheckValidAmount(null, 1.0); // here i have mentioned NULL
        Assert.AreEqual(dd, 9.0);
    }
}

预计将通过:

[TestClass]
public class SampleTester
{
    [TestMethod]
    [ExpectedException(typeof(NullReferenceException))]
    public void CheckValidAmount()
    {
        SampleTestClass sp = new SampleTestClass();
        double dd = sp.CheckValidAmount("RamKumar", 1.0);
        Assert.AreEqual(dd, 9.0);
    }
}

最后一个应该通过..但总是显示出失败...

参考:

   ExpectedExceptionAttribute Class : 
   https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.expectedexceptionattribute.aspx

我的VS显示了此消息:

         ![enter image description here][1]

请指导我理解这一点..谢谢

https://i.stack.imgur.com/g2r1b.png

当您要验证方法是否抛出正确的异常时,应该使用ExpectionExceptionAttribute。在您的情况下,您不会(从您的方法出发)。您的方法行为是:

给定金额为1.0

我用无效的名称(null名称)

检查金额

然后我应该获得1.0作为量

从我的GWT中可以看到,您的方法行为不会期望引发任何例外。因此您的测试失败了。

    [TestMethod]
    [ExpectedException(typeof(NullReferenceException))]// remove this attribute to pass the test 
    public void CheckValidAmount()
    {
        SampleTestClass sp = new SampleTestClass();
        double dd = sp.CheckValidAmount("RamKumar", 1.0);
        Assert.AreEqual(dd, 9.0); 
    }

当您的方法行为类似于:

时,您应该使用ExpectexceptionAttribute

给定金额为1.0

我用无效的名称(null名称)

检查金额

然后参数nullexception 应提高

在这种情况下,您的方法是:

    public double CheckValidAmount(object Name, double amount)
    {
            if (Name == null)
                throw new ArgumentNullException("Name");
            if (amount == 1.0 && Name.ToString() == "RamKumar")
                return 10.0 - amount;
            return amount;
    }

您的测试方法应为:

    [TestMethod]
    [ExpectedException(typeof(ArgumentNullException))]
    public void CheckValidAmount_CallWithInvalidName_Throw()
    {
        SampleTestClass sp = new SampleTestClass();
        sp.CheckValidAmount("RamKumar", 1.0);
    }

您的测试单元不会引发异常,因为您将其捕获到其中。因此单位测试看不到。

决定是否抛出异常并这样测试或捕获它,不要期望异常。

此测试...

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void CheckValidAmount()
{
    SampleTestClass sp = new SampleTestClass();
    double dd = sp.CheckValidAmount("RamKumar", 1.0);
    Assert.AreEqual(dd, 9.0);
}

永远不会通过以下实施...

public double CheckValidAmount(object Name , double amount)
{
    try
    {
        if (amount == 1.0 && Name.ToString() == "RamKumar")
            return 10.0 - amount;
        else
            return amount;
    }
    catch (ArgumentNullException ex)
    {
        return amount;
    }
}

由于该测试期望有一个参数nullexception,因此实现不会为给定实现提供参数。删除ExpectedException属性,您的测试应通过

最新更新