using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Maths
{
public class Mathe
{
public int Add(int a,int b)
{
if (IsNegative(a) && IsNegative(b))
{
throw new Exception("Number should not be negative");
}
return a + b;
}
private bool IsNegative(int num)
{
if (num < 0)
return true;
else
return false;
}
}
}
--------------------------------------------------
Unit Test for private methods using reflections
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Maths;
namespace UnitTestProject1
{
[TestClass]
public class CalculatorTest
{
[TestMethod]
public void TestAdd()
{
//Arrange
PrivateObject obj = new PrivateObject(typeof(Mathe));
bool result = Convert.ToBoolean(obj.Invoke("IsNegative", -2));
Assert.AreEqual(true,result);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Maths
{
public class Mathe
{
public int Add(int a,int b)
{
if (IsNegative(a) && IsNegative(b))
{
throw new Exception("Number should not be negative");
}
return a + b;
}
private bool IsNegative(int num)
{
if (num < 0)
return true;
else
return false;
}
}
}
--------------------------------------------------
Unit Test for private methods using reflections
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Maths;
namespace UnitTestProject1
{
[TestClass]
public class CalculatorTest
{
[TestMethod]
public void TestAdd()
{
//Arrange
PrivateObject obj = new PrivateObject(typeof(Mathe));
bool result = Convert.ToBoolean(obj.Invoke("IsNegative", -2));
Assert.AreEqual(true,result);
}
}
}
No comments:
Post a Comment