请帮助我:
在这里,我需要通过Add(...)
中的Calling(...)
方法调用此MyAnotherClass
方法。当我Assert
时出现错误。请给我看看路。
public class MyClass
{
public List<int> number = new List<int>();
public void Add(int a, int b)
{
int c = a + b;
number.Add(c);
}
}
public class MyAnotherClass
{
public void CallingMethod(int c, int d)
{
MyClass mc = new MyClass();
mc.Add( c, d);
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
MyAnotherClass mac = new MyAnotherClass();
MyClass mc = new MyClass();
mc.Add(2, 3);
Assert.AreEqual(5, mc.number[0]);// **this work fine**
mac.CallingMethod(2, 3);
Assert.AreEqual(5, mc.number[0]);// **but this not**
}
}
谢谢
初学者
最佳答案
您的类MyAnotherClass返回void,并且不提供可以从内部MyClass检索数据的公共属性。
您想要以下内容:
public class MyAnotherClass
{
public MyClass mc = new MyClass();
public void CallingMethod(int c, int d)
{
mc.Add(c, d);
}
}
然后,在测试类中,您将在比较中调用mac.mc.number [0]。
关于c# - 单元测试从C#中的另一个方法调用方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8757944/