mercredi 6 mai 2015

ExpectedException attribute fails to show the expected result C#

I have created a Class library .

I have created unit test project to unit test a class.

I have gone through a particular class called

  " ExpectedExceptionAttribute Class "

I tried to implement it. But if i enable the attribute my code always shows failed. Even if data are correct.

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 (ArgumentNullException ex)
        {
            return amount;
        }
    }
}

Unit Test Pass :

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

        Assert.AreEqual(dd, 9.0);
    }
}

Fail :

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

Expected to be passed :

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

        Assert.AreEqual(dd, 9.0);
    }
}

Last one should be passed .. But it always shows Failed...

Reference :

   ExpectedExceptionAttribute Class : 
   http://ift.tt/1H0MpNE

My VS shows this message :

         ![enter image description here][1]

Kindly guide me to understand this.. Thanks

http://ift.tt/1FQMXHg

Aucun commentaire:

Enregistrer un commentaire