单元 - 测试函数

时间:2015-05-14 13:32:16

标签: c# unit-testing

考虑以下代码

public class MyClass
{

public string FirstName { get; set; }
public string SurName { get; set; }

public MyClass GetInfo()
{
    //Get data from database 
    ...
    return new MyClass(FirstName, SurName);
}
}

我想单元测试GetInfo()但似乎无法弄清楚如何实际测试它。我是否会创建MyClass的模拟,然后执行Assert.AreEqual(uinfo.FirstName,"John");

之类的操作

提前感谢您的帮助和支持

1 个答案:

答案 0 :(得分:1)

[TestMethod]
public void GetInfo_ClonesAllProperties()
{
    // arrange
    var myClass = new myClass() { FirstName = "John", SurName = "Smith" };

    // act
    var clone = myClass.GetInfo();

    // assert
    Assert.AreEqual(clone.FirstName,"John");
    Assert.AreEqual(clone.SurName,"Smith");
}